Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Abrufen des neuesten Datensatzes für jeden Kunden

;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;

Vermeiden Sie reservierte Wörter (und vage Spaltennamen) wie [DATE] ...