开发者

Find Duplicates using Rank Over Partition

开发者 https://www.devze.com 2023-04-07 17:58 出处:网络
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.

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

精彩评论

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

关注公众号