如何删除SQLServer中重复的行?我怎么能删除重复行哪里没有unique row id存在吗?我的桌子是col1 col2 col3 col4 col5 col6 col7john 1 1 1 1 1 1 john 1 1 1 1 1 1sally 2 2 2 2 2 2sally 2 2 2 2 2 2在重复删除后,我想留下以下内容:john 1 1 1 1 1 1sally 2 2 2 2 2 2我尝试过一些查询,但我认为它们依赖于有一个行id,因为我没有得到想要的结果。例如:DELETEFROM tableWHERE col1 IN ( SELECT id FROM table GROUP BY id HAVING (COUNT(col1) > 1))
3 回答
隔江千里
TA贡献1906条经验 获得超10个赞
ROW_NUMBER
DELETE FROM CTE...
SELECT * FROM CTE
:
WITH CTE AS( SELECT [col1], [col2], [col3], [col4], [col5], [col6], [col7], RN = ROW_NUMBER()OVER(PARTITION BY col1 ORDER BY col1) FROM dbo.Table1)DELETE FROM CTE WHERE RN > 1
演示
COL1 COL2 COL3 COL4 COL5 COL6 COL7 john 1 1 1 1 1 1sally 2 2 2 2 2 2
col1
PARTITION BY col1
PARTITION BY
:
ROW_NUMBER()OVER(PARTITION BY Col1, Col2, ... ORDER BY OrderColumn)
素胚勾勒不出你
TA贡献1827条经验 获得超9个赞
CTE
ROW_NUMBER()
MAX
DELETEFROM MyDuplicateTableWHERE ID NOT IN(SELECT MAX(ID)FROM MyDuplicateTableGROUP BY DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)
添加回答
举报
0/150
提交
取消