当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]
更改RANK为ROW_NUMBER。
RANK
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]