删除 SQL 服务器中重复的最大行
Delete duplicate max row in SQL server
我想删除 SQL 中的重复行。我的查询可以删除 RN > 1
的重复行,但我想删除 max(RN)
:
WITH CTE AS(
SELECT [col1], [col2], [col3]
RN = ROW_NUMBER()OVER(PARTITION BY [col1], [col2] ORDER BY [col1], [col2])
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
只需将分区更改为与当前顺序相反的顺序,然后只删除具有第一行号的记录:
WITH CTE AS (
SELECT [col1], [col2], [col3]
RN = ROW_NUMBER() OVER (PARTITION BY [col1], [col2] ORDER BY [col1] DESC, [col2] DESC)
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN = 1;
我想删除 SQL 中的重复行。我的查询可以删除 RN > 1
的重复行,但我想删除 max(RN)
:
WITH CTE AS(
SELECT [col1], [col2], [col3]
RN = ROW_NUMBER()OVER(PARTITION BY [col1], [col2] ORDER BY [col1], [col2])
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN > 1
只需将分区更改为与当前顺序相反的顺序,然后只删除具有第一行号的记录:
WITH CTE AS (
SELECT [col1], [col2], [col3]
RN = ROW_NUMBER() OVER (PARTITION BY [col1], [col2] ORDER BY [col1] DESC, [col2] DESC)
FROM dbo.Table1
)
DELETE FROM CTE WHERE RN = 1;