开发者

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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜