这是我的数据模型
ProjectsTable DocumentTable DocumentVersionsTable UserTable DocumentVersionActivityTable
ProjectID(PK) DocumentID(PK) DocumentVersionID(PK) UserID ActivityID(PK)
ProjectID(FK) DocumentID(PK) DocumentVersionID(FK)
UserID(FK)
DateOfActivity
需要获得的是所有不同的项目,其中特定用户创建了DocumentVersionActivity,按DateOfActivity降序排序(例如,编辑的最新文档版本将反映为最近编辑的项目。
我使用了以下内容,它成功跟踪了DocumentVersion到项目,但我不确定如何通过User限制它并基于DocumentVersionActivityTable进行排序
select a.projectid
b.documentid
c.documentversionid
from DocumentVersions c
join Documents b on b.ID = c.DocumentID
join Projects a on a.ID = b.ProjectID
我正在使用EF 6.0为ASP.Net Web API项目做这个,所以如果我可以在没有SQL的情况下轻松或快速地捕获它们,我也很乐意看到它。
感谢。
最终编辑: 我要使用的代码(注意:请注释掉项目以供参考)。谢谢Leslie:
SELECT a.ID as projectid ,
-- b.ID as haldocumentid,
-- c.ID as documentversionid,
--d.projectcomponentActivityID as projectcomponentactivityid,
-- e.id as userid,
max(d.datetimeofactivity) as latestdate
FROM DocumentVersions c
INNER JOIN halDocuments b ON b.ID = c.DocumentID
INNER JOIN Projects a ON a.ID = b.ProjectID
INNER JOIN Documentversionprojectcomponentactivities d ON
d.AssociatedDocumentVersionID = c.ID
INNER JOIN Users e ON e.id = d.userid
WHERE e.id = 3 and d.DateTimeOfActivity >= DATEADD(d, -30, getdate())
group by a.id --, b.id, c.id, e.id
order by latestdate desc`
答案 0 :(得分:1)
编辑:
SELECT
--a.ID as projectid,
b.ID as documentid,
c.ID as documentversionid,
d.projectcomponentActivityID as projectcomponentactivityid,
e.id as userid,
MAX(d.DatetimeOfActivity)
FROM DocumentVersions c
INNER JOIN Documents b ON b.ID = c.DocumentID
--INNER JOIN Projects a ON a.ID = b.ProjectID
INNER JOIN Documentversionprojectcomponentactivities d
ON d.AssociatedDocumentVersionID = c.ID
INNER JOIN Users e ON e.id = d.userid
WHERE e.id = 2
GROUP BY
--a.ID as projectid,
b.ID as documentid,
c.ID as documentversionid,
d.projectcomponentActivityID as projectcomponentactivityid,
e.id as userid
--ORDER BY d.DatetimeOfActivity DESC