2009-04-09 44 views
2

給下面這個簡單的表結構:多計數

Departments 
PK - DeptID DeptName 
-------------------------- 
1    Department 1 
2    Department 2 
3    Department 3 
4    Department 4 

Groups 
PK - GroupdID DeptID 
-------------------------- 
1    1 
2    1 
3    3 
4    4 
5    2 
6    3 
7    1 
8    3 

Inventory 
PK - ItemID GroupID 
-------------------------- 
1    2 
2    3 
3    8 
4    1 
5    4 
6    5 
7    1 
8    2 
9    2 
10    3 
11    7 

有不使用子查詢(很簡單)的方式在那裏我能得到有關部門的列表,計數每個部門的組別,以及每個部門的庫存數量?

輸出示例:

DeptID DeptName   GroupCount  ItemCount 
----------------------------------------------------- 
1   Department 1  3    6 
2   Department 2  1    1 
3   Department 1  3    3 
4   Department 4  1    1  

我的直覺告訴我,這是得到GROUP BY語句糾正的只是一個簡單的事情,但到目前爲止,我畫一個空白。如果它確實需要使用子查詢,這不是問題。我只是想確認以供將來參考。

注意:使用SQL Server 2000爲這個特殊問題

+0

@Lieven:感謝您的標籤編輯。我很習慣在2005年工作,標籤已成爲本能。 – TheTXI 2009-04-09 13:57:23

+0

@TheTXI:不要提及它。順便說一句,我相信部門2的團隊人數和itemcount應該是1?! – 2009-04-09 14:06:37

+0

部門2爲什麼組計數爲0? – Quassnoi 2009-04-09 14:07:16

回答

11
SELECT d.deptID, 
     COUNT(DISTINCT g.GroupID) AS Groups, 
     COUNT(DISTINCT i.ItemID) AS Items 
FROM Departments d 
LEFT JOIN 
     Groups g 
ON  g.deptID = d.deptID 
LEFT JOIN 
     Items i 
ON  i.GroupID = g.GroupID 
GROUP BY 
     d.deptID 

產生的結果是:

deptID Groups Items 
----- ------ ----- 
1  3  6 
2  1  1 
3  3  3 
4  1  1 

這也將產生正確的0的針對沒有Groups,或僅具有Groups而不ItemsDepartments

0

對不起,我不是坐在我的SQL Server的前面。這是你的第一次嘗試。我可能無法正確理解你想要的結果,但也許你可以用它作爲一個起點?

SELECT 
    Department.DeptId, 
    Department.DeptName, 
    Group.GroupId, 
    COUNT (Inventory.GroupId) as TotalItems 
FROM 
    Department 
    INNER JOIN Groups 
    On (Department.DeptId = Groups.DeptId) 
    INNER JOIN Inventory 
    On (Inventory.GroupId = Groups.GroupId) 
GROUP BY 
    Department.DeptId, 
    Department.DeptName 
    Group.GroupId, 
    Inventory.GroupId 
1

以下是得到的結果至少有一種方式。

SELECT d.DeptID, d.DeptName, ISNULL(g.Groups, 0), ISNULL(t.TotalItems, 0) 
FROM 
    Departments d 
    LEFT OUTER JOIN (
    SELECT d.DeptID, Groups = COUNT(*) 
    FROM Departments d 
     INNER JOIN Groups g ON g.DeptID = d.DeptID 
    GROUP BY d.DeptID 
) g ON g.DeptID = d.DeptID 
    LEFT OUTER JOIN (
    SELECT d.DeptID, TotalItems = COUNT(*) 
    FROM Departments d 
     INNER JOIN Groups g ON g.DeptID = d.DeptID 
     INNER JOIN Inventory i ON i.GroupID = g.GroupID 
    GROUP BY d.DeptID 
) t ON t.DeptID = d.DeptID 
1

這裏是我的嘗試......

declare @Depatments table 
(
DeptID int 
,DeptName varchar(15) 
) 

declare @Groups table 
(
GroupID int 
,DeptID int 
) 

declare @Inventory table 
(
ItemID int 
,GroupID int 
) 

INSERT INTO @Depatments VALUES (1,'Department 1') 
INSERT INTO @Depatments VALUES (2,'Department 2') 
INSERT INTO @Depatments VALUES (3,'Department 3') 
INSERT INTO @Depatments VALUES (4,'Department 4') 

INSERT INTO @Groups VALUES (1,1) 
INSERT INTO @Groups VALUES (2,1) 
INSERT INTO @Groups VALUES (3,3) 
INSERT INTO @Groups VALUES (4,4) 
INSERT INTO @Groups VALUES (5,2) 
INSERT INTO @Groups VALUES (6,3) 
INSERT INTO @Groups VALUES (7,1) 
INSERT INTO @Groups VALUES (8,3) 

INSERT INTO @Inventory VALUES (1 ,2) 
INSERT INTO @Inventory VALUES (2 ,3) 
INSERT INTO @Inventory VALUES (3 ,8) 
INSERT INTO @Inventory VALUES (4 ,1) 
INSERT INTO @Inventory VALUES (5 ,4) 
INSERT INTO @Inventory VALUES (6 ,5) 
INSERT INTO @Inventory VALUES (7 ,1) 
INSERT INTO @Inventory VALUES (8 ,2) 
INSERT INTO @Inventory VALUES (9 ,2) 
INSERT INTO @Inventory VALUES (10,3) 
INSERT INTO @Inventory VALUES (11,7) 


--works with derived tables 
SELECT 
    d.DeptName,dt_g.CountOf AS GroupCount, dt_i.CountOf AS InventotyCount 
    FROM @Depatments d 
     LEFT OUTER JOIN (SELECT 
          COUNT(*) AS CountOf,DeptID 
          FROM @Groups 
          GROUP BY DeptID 
         ) dt_g ON d.DeptID=dt_g.DeptID 
     LEFT OUTER JOIN (SELECT 
          COUNT(*) AS CountOf,g.DeptID 
          FROM @Groups    g 
           INNER JOIN @Inventory i ON g.GroupID=i.GroupID 
          GROUP BY DeptID 
         ) dt_i ON d.DeptID=dt_i.DeptID