我正在搜索删除表上的重复条目,我看到了如下示例:
CREATE TABLE Suppliers
(
Id int identity (1,1),
CompanyTitle nvarchar(1000),
ContactName nvarchar(100),
LastContactDate datetime
)
INSERT Suppliers VALUES (N'Melody Music Instruments',N'James Manning', '20090623 10:15')
INSERT Suppliers VALUES (N'Blue Jazz',N'Mike Clark', '20090720 15:40')
INSERT Suppliers VALUES (N'Top Music',N'Katy Swan', '20090827 18:00')
INSERT Suppliers VALUES (N'Blue Jazz',N'Mike Clark', '20090806 10:00')
INSERT Suppliers VALUES (N'Melody Music Instruments',N'James Brown', '20080121 11:20')
INSERT Suppliers VALUES (N'Top Music',N'Katy Perry', '20090825 14:00')
INSERT Suppliers VALUES (N'Top Music',N'Katy Perry', '20090825 14:00')
WITH Duplicate AS
(
SELECT
RN = ROW_NUMBER() OVER (PARTITION BY CompanyTitle ORDER BY LastContactDate DESC)
FROM Suppliers
)
delete from Duplicate where RN > 1
CTE 返回类似的东西,然后如果值大于 1 则删除。
RN
--
1
2
1
2
1
2
3
我不明白的是它如何理解哪个条目将被删除。它只是通过这个例子返回重复的条目计数。