Find Duplicates using Rank Over Partition
The following SQL works in identifying unique phones when there is a disparity in LastDate. But if duplicate phones have the exact same LastDate it does not work.
Any ideas will be appreciate it.
SELECT * FROM
(
SELECT ID, Phone, [开发者_开发技巧LastDate]
,RANK() OVER (PARTITION BY Phone ORDER BY [LastDate]) AS 'RANK',
COUNT(Phone) OVER (PARTITION BY Phone) AS 'MAXCOUNT'
FROM MyTable
WHERE Groupid = 5
) a
WHERE [RANK] = [MAXCOUNT]
Change the RANK
for ROW_NUMBER
.
SELECT *
FROM ( SELECT ID, Phone, [LastDate],
ROW_NUMBER() OVER (PARTITION BY Phone ORDER BY [LastDate]) AS 'RANK',
COUNT(Phone) OVER (PARTITION BY Phone) AS 'MAXCOUNT'
FROM MyTable
WHERE Groupid = 5) a
WHERE [RANK] = [MAXCOUNT]
精彩评论