Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Rechercher des doublons à l'aide du classement sur la partition

Changer le RANK pour 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]