我从其他问题尝试了一些解决方案,但它们效果不佳。 我有一个包含2列的表,我需要删除行重复项。
Column1 --- Column2 1 ---------------- 3 1 ---------------- 1 2 ---------------- 1 1 ---------------- 1 (remove this)
我找到了重复的内容:
select c1,c2, count(*)
from table
group by c1,c2
having count(*) > 1
如何删除重复项并保留一个条目? (sql server 2008r2)
答案 0 :(得分:2)
一种选择是将common table expression与row_number:
一起使用;WITH CTE AS
(
SELECT Col1, Col2, ROW_NUMBER() OVER (PARTITION BY Col1, Col2 ORDER BY Col1, Col2) As rn
FROM @MyTable
)
DELETE
FROM CTE
WHERE rn > 1
SELECT *
FROM @MyTable