复制101从基本搜索到删除?

时间:2014-07-13 05:48:20

标签: sql sql-server-2008 duplicate-removal delete-row

目标是找到重复项。

Select Column1, Column2, Column3 
from Tablename
where Column1 = 1111 and Column2 = 2222
group by Column1, Column2, Column3 
having count(*) > 1

select查询查找所有重复的行。我想将它们全部删除,因此我将select更改为delete

Delete from Tablename
where Column1 = 1111 and Column2 = 2222
group by Column1, Column2, Column3 
having count(*) > 1

我收到一条错误消息,说我无法使用" group by"。

删除所有内容的另一个选择是什么?

1 个答案:

答案 0 :(得分:0)

尝试此查询:

with v1 as
(select 
 ROW_NUMBER() OVER (ORDER BY Column1) AS ID,t1.*
FROM Tablename t1)
DELETE FROM v1 where Column1=1111 and Column2=2222
and id not in (select min(id) from v1 group by
              Column1, Column2, Column3);

SQL Fiddle