我想跟蹤工作中不同部門進行了多少次調查。 通過使用「分組依據」並計算匹配數量,這相當容易。在sql中計數匹配
SELECT Departments.DepartmentName, count(*) as [survey count]
FROM Departments INNER JOIN
Surveys ON Departments.DepartmentID = Surveys.DepartmentID
GROUP BY Departments.DepartmentName
但這隻顯示已完成調查的部門。我如何讓未完成調查的部門在結果清單中顯示爲零計數?
更新:
SELECT Departments.DepartmentName AS Department,
COUNT(Surveys.DepartmentID) AS [survey count]
, Departments.DepartmentID
FROM Surveys FULL OUTER JOIN
Departments ON Surveys.DepartmentID = Departments.DepartmentID
GROUP BY Departments.DepartmentName, Departments.DepartmentID
感謝 - 它沒有exacly工作,如你所說,直到我把的只是「FULL OUTER JOIN」 INSEAD 「LEFT JOIN」 – Brad 2009-08-29 18:55:21
再次仔細檢查結果。完全外部連接的目的不同於你以後的目的 – 2009-08-29 19:16:47