Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Requête SQL - Obtenir la révision la plus récente

La requête ci-dessous fonctionnera indépendamment de toute relation entre ProjectRevId et DateCreated.

SELECT *
FROM ProjectRevisions
INNER JOIN (
  SELECT ProjectId
    , MAX(DateCreated) AS DateCreated
  FROM  ProjectRevisions
  GROUP BY ProjectId
  ) AS CurrentRevision
  ON CurrentRevision.ProjectId = ProjectRevisions.ProjectId
  AND CurrentRevision.DateCreated = ProjectRevisions.DateCreated