Monday, July 1, 2019

How to get duplicate records




;WITH cte 
AS
(
SELECT DISTINCT d.company_fk, d.id,  d.name 
--, ROW_NUMBER() OVER(PARTITION by t.FName, t.LName ORDER BY t.FName, t.LName,  t.Sex DESC) AS duplicateRecCount
,COUNT(d.id) OVER (PARTITION BY  d.name, d.company_fk, d.[status] ) AS [max_count]
FROM tbl_department d
WHERE [status] =  'A'


SELECT * FROM cte WHERE max_count > 1 
ORDER BY name

No comments: