WITH Data AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Date DESC) AS Row_Number,
ID,
AverageValue
FROM Table
)
SELECT * FROM Data WHERE Row_Number = 1
Esta es la forma en que debe hacerlo en SQL Server. Hace que "último/primero/máximo/mínimo/mayor-n-por grupo" sea pan comido.
PARTITION BY
palabra clave aquí es igual a su GROUP BY
declaración, y el ORDER BY
aquí se traduciría en MAX
función agregada que usó en MySQL
Usar esto te permitiría hacer cosas sofisticadas como:
WITH Data AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Date DESC) AS Row_Number,
ID,
AverageValue
FROM Table
)
SELECT Newest.ID,
Newest.AverageValue,
(Newest.AverageValue - Next_Newest.AverageValue) AS AverageValue_Change,
Newest.Date
FROM Data Newest
LEFT OUTER JOIN Data Next_Newest
ON Newest.Row_Number+1 = Next_Newest.Row_Number
AND Newest.ID = Next_Newest.ID
WHERE Newest.Row_Number=1