I need to be able to remove some rows in a table where two-column combination have the same value. For example, in below sample table, there should be only one combination of (48983, 2018-05-01).
ID CertID DueDate
676790 48983 2018-05-03
678064 48983 2018-05-02
678086 48983 2018-05-01
678107 48983 2018-05-01
678061 48983 2018-05-01
I tried to get the list of duplicate entries but what I get is the entire table. This is what I used:
WITH A -- Get a list of unique combinations of ResponseDueDate and CertificateID
AS (
SELECT Distinct
ID,
ResponseDueDate,
CertID
FROM FacCompliance
)
, B -- Get a list of all those CertID values that have more than one ResponseDueDate associated
AS (
SELECT CertID
FROM A
GROUP BY
CertID
HAVING COUNT(*) > 1
)
SELECT A.ID,
A.ResponseDueDate,
A.FacCertificateID
FROM A
JOIN B
ON A.CertID = B.CertID
order by CertID, ResponseDueDate;
What is wrong with the query I am using and is it possible to remove extra rows (in above example, keep one instance of (48983, 2018-05-01) combination and remove the rest. I am using SQL Server 2016.
ROW_NUMBER()OVER(Partition by CertId,DueDate ORDER BY ID, then delete the row number >1