我有四个表:User,Version,VersionOfDocument,Document。 ER如下所示:(粗体=主键)
我希望查询为每个documentID显示最近的日期,versionNr以及最后一次更改文档的人:
documentID modifiedDate Username VersionNr
1 1/4/2014 Alfred.Heymans@test.nl 2.0
2 2/5/2013 Alfred.Heymans@test.nl 1.0
3 4/5/2014 John.Smith@test.nl 2.0
答案 0 :(得分:0)
试试这个:
SELECT VD.DocumentID, V.modifiedDate, U.Username, V.VersionNr
FROM VersionOfDocument VD
INNER JOIN VERSION V ON VD.VersonId = V.VersionId
INNER JOIN (SELECT VD.DocumentID, MAX(V.modifiedDate) modifiedDate
FROM VersionOfDocument VD
INNER JOIN VERSION V ON VD.VersonID = V.VersionID
GROUP BY VD.DocumentID
) AS A ON VD.DocumentID = A.DocumentID AND V.modifiedDate = A.modifiedDate
INNER JOIN USER U ON V.userID = U.userID;