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

T SQL - Eloquenter Ersatz von Correlated Subquery

Ich würde es mit einem CTE machen:

WITH Result AS
(
  SELECT Row_Number() OVER (PARTITION BY ItemId, Year
ORDER BY ItemversionId DESC) AS RowNumber
      ,ItemId
      ,ItemversionId
      ,Year
      ,Value
  FROM table
)
SELECT ItemId
  ,ItemversionId
  ,Year
  ,Value
FROM Result
WHERE RowNumber = 1
ORDER BY ItemId, Year