SQL查询中的多重计数

发布于 2024-07-16 14:56:39 字数 969 浏览 2 评论 0原文

给出以下简单的表结构:

<前><代码>部门 PK - 部门 ID 部门名称 -------------------------- 1 部门 1 2 部门 2 3 部门3 4 部门 4 团体 PK - GroupdID 部门 ID -------------------------- 1 1 2 1 3 3 4 4 5 2 6 3 7 1 8 3 存货 PK - 物品 ID 组 ID -------------------------- 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 解决此特定问题

Give the following simple table structure:

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

Is there a way without using subqueries (which is easy) where I could get a listing of the departments, the count of the groups in each department, and the count of the inventory in each department?

Example Output:

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

My gut is telling me it's just a simple matter of getting the GROUP BY statements correct, but so far I'm drawing a blank. If it does require the use of subqueries, this isn't a problem. I just wanted to confirm for future reference.

NOTE: Using SQL Server 2000 for this particular problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帅哥哥的热头脑 2024-07-23 14:56:40
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

对于没有 Groups 或仅具有 Groups< 的 Departments,这也会生成正确的 0。 /code> 没有 Items

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

The results produced are:

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

This will also produce correct 0's for the Departments that have no Groups, or having only Groups without Items.

淡水深流 2024-07-23 14:56:40

以下至少是获得结果的一种方法。

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

Following is at least one way to get the results.

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
挽清梦 2024-07-23 14:56:40

这是我的尝试...

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

Here's my try...

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
倾城°AllureLove 2024-07-23 14:56:40

抱歉,我没有坐在我的 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

Sorry I am not sitting in front of my SQL Server. Here is a first attempt for you. I might not understand you desired result correctly but maybe you can use this as a starting point?

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文