I have a table that has
Account, CompanyID, Mail_Address
1001,1,Main ST
1001,2,Main ST
1001,3,Florida ST
1002,1,South Main
1002,2,West Main
1002,3,Soth Main
1003,1,North Main
1003,2,North Main
I need to create a query that removes duplicate Mail_Address, which is easy enough. The problem is that the results returned are the last of the duplicated value, instead of the first duplicated value.
If my table was the above the results would be
Account, CompanyID, Mail_Address
1001,2,Main ST
1001,3,Florida ST
1002,2,West Main
1002,3,South Main
1003,2,North Main
Notice the query deleted the first records and kept the last of the duplicated values.
I'd perfer the outcome to be
Account, CompanyID, Mail_Address
1001,1,Main ST
1001,3,Florida ST
1002,1,South Main
1002,2,West Main
1003,1,North Main
My Query
SELECT *
FROM (SELECT [ACCOUNT]
,[COMPANY]
,[MAIL_ADDRESS]
,
ROW_NUMBER() OVER(PARTITION BY Mail_Address ORDER BY Account ASC) rn
FROM [SQLUserDB].[dbo].[DSDD01]
) a
WHERE rn = 1