SQL Server : find duplicates in a table based on values in a single column
11,039
select
employeename,
IDs = STUFF((SELECT ','+ CAST(e2.[ID] AS VARCHAR(10))
FROM emp e2
WHERE e2.employeename = e1.employeename
For XML PATH('')
),1,1,'')
FROM emp e1
GROUP BY employeename having COUNT(*) > 1
Author by
slayernoah
SO has helped me SO much. I want to give back when I can. And I am #SOreadytohelp http://stackoverflow.com/users/1710577/slayernoah #SOreadytohelp
Updated on June 04, 2022Comments
-
slayernoah almost 2 years
I have a SQL Server table with the following fields and sample data:
ID employeename 1 Jane 2 Peter 3 David 4 Jane 5 Peter 6 Jane
The
ID
column has unique values for each row.The
employeename
column has duplicates.I want to be able to find duplicates based on the
employeename
column and list theID
s of the duplicates next to them separated by commas.Output expected for above sample data:
employeename IDs Jane 1,4,6 Peter 2,5
There are other columns in the table that I do no want to consider for this query.
Thanks for all your help!
-
slayernoah almost 11 yearsThanks @EricZ! This is what I was looking for!
-
Helen Craigman almost 11 yearsWhat about using CTE? "with CTE as ( select row_number() over(partition by employeename order by ID ) as rn from table )" - how to continue?
-
EricZ almost 11 years@HelenCraigman, not sure what you want to do with CTE that query show row number. for string concatenation, FOR XML is the better way.