我有下表:
Case with columns > ID,ResponsibleID
Action with columns > ID,CaseID,Action
我希望得到一份所有责任人的清单,列出所有案件数量及其在所有案件中的总行动次数。
这可能是查询是否有效,但它不是
Select
Case.ResponsibleID,
CaseCount=count(*),
--how can i tell sql that this is the CaseID sublist of the group?
--It would be possible with a subquery, but i rather not do that for performance reasons.
--the real query gets the caseID list from a table 3 or 4 joins later.
ActionCount=(select count(*) from Action where CaseID in Case.CaseID)
From Case
Group by ResponsibleID
答案 0 :(得分:1)
SELECT c.ResponsibleID
, COUNT(DISTINCT c.ID)
, COUNT(*)
FROM Case c
LEFT JOIN Action a ON a.CaseID = c.CaseID
GROUP BY
c.ResponsibleID