小编典典

使用“按分区排名”查找重复项

sql

当LastDate中存在差异时,以下SQL可用于识别唯一的电话。但是,如果重复的电话具有完全相同的LastDate,则它将不起作用。

任何想法将不胜感激。

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]

阅读 175

收藏
2021-04-22

共1个答案

小编典典

更改RANKROW_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]
2021-04-22