Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

recuperare il record più recente per ciascun cliente

;WITH x AS 
(
    SELECT ID, NAME, [DATE], 
      rn = ROW_NUMBER() OVER 
      (PARTITION BY NAME ORDER BY [DATE] DESC)
    FROM @TESTABLE
)
SELECT ID, NAME, [DATE] FROM x WHERE rn = 1
  ORDER BY [DATE] DESC;

Cerca di evitare parole riservate (e nomi di colonne vaghi) come [DATE] ...