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
Sådan skal du gøre det i SQL Server. Det gør "sidste/første/maks./min./største-n-pr. gruppe" let som en kage.
PARTITION BY
søgeord her er lig med din GROUP BY
sætning og ORDER BY
her ville oversætte til MAX
aggregatfunktion, du brugte i MySQL
Ved at bruge dette vil du gøre nogle smarte ting som:
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