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

Zeilen auswählen, die das gruppenweise Maximum einer Spalte enthalten

Windowing-Funktionen

WITH t AS (
  SELECT *
   ,MAX(date) OVER(PARTITION BY id) max_date_by_id
  FROM Item
)
SELECT *
FROM t
WHERE date = max_date_by_id

Kreuzweise anwenden

SELECT *
FROM Item t
CROSS APPLY (
  SELECT MAX(date) max_date_by_id
  FROM Item
  WHERE id = t.id
)
WHERE date = max_date_by_id