Wednesday, 28 January 2015

SELECT DISTINCT does not work with multiple columns

Hi Folks,
Today I came across a scenario where select distinct did not work when more than two columns are used. To resolve this issue, I used the below type of CTE using Row_Number



;WITH DistinctMails AS
(
    SELECT ID, MailID, EMailAddress, NAME,
        ROW_NUMBER() OVER(PARTITION BY EMailAddress ORDER BY ID) AS 'RowNum'
    FROM dbo.YourMailTable
)
SELECT *
FROM DistinctMails
WHERE RowNum = 1
And it worked !!!

No comments:

Post a Comment