我有一个表,其中有多个列需要删除重复数据,但是我需要为每行保留现有的主键。显然,当运行没有主键列(Source_ID)的SELECT DISTINCT ....时,排除主键列时会提供所需的结果。
所以我如何获取不同的行并维护现有的主键数据。
Source table before de-dupe
Source_ID Title First_Name Last_Name Organisation
1000013 Mr James Caine Company1
1000014 Ms Judith Jason Company2
1000015 Mr James Caine Company1
table after de-dupe
Source_ID Title First_Name Last_Name Organisation
1000013 Mr James Caine Company1
1000014 Ms Judith Jason Company2
答案 0 :(得分:2)
使用ROW_NUMBER()
如下:
SELECT Source_ID, Title, First_Name, Last_Name, Organisation
FROM
(SELECT *,ROW_NUMBER() OVER(Partition by Title, First_Name, Last_Name, Organisation Order by Source_ID) as rn
FROM YourTable) as tblMain
WHERE rn = 1
答案 1 :(得分:1)
也许您只想聚合:
select max(source_id) as source_id,
Title, First_Name, Last_Name, Organisation
from t
group by Title, First_Name, Last_Name, Organisation;
答案 2 :(得分:1)
您可以使用“从此处查找重复值”
;WITH cte AS
(SELECT Source_ID, Title, First_Name, Last_Name, Organisation,
rn=ROW_NUMBER() OVER(Partition by Title, First_Name, Last_Name, Organisation Order by Source_ID
FROM YourTable)
SELECT *
FROM WHERE rn > 1