删除重复次数最多的记录
表tb中有3个字段,其中有很多重复记录,现在想把重复次数最多的记录删除,可用下面的命令把所有的记录都删除了,请大虾们帮帮忙,谢谢。
DELETE FROM tb WHERE EXISTS(SELECT MAX(fc) FROM (SELECT ax,ay,az,COUNT(*) fc FROM tb GROUP BY ax,ay,az) a)
------解决方案--------------------
SQL code
delete a
from tb a
inner join
(select top 1 ax,ay,az
from tb
group by ax,ay,az
order by count(1) desc
) b on a.ax=b.ax and a.ay=b.ay and a.az=b.az