如何获得相同单位的总和

时间:2014-08-25 07:31:28

标签: sql sql-server

我有一张桌子,其中包含学院的部门及其单位和子单元。

OrganizationID  ParentUnit  Unit    ChildUnit   UnitName
            10           1     0            0   Education
            12           1     1            0   Sports
            24           1     2            0   Mathmatics
            28           1     3            0   Science
            35           1     3            1   Physics
            51           1     4            0   Arts
            66           1     4            1   Music
            69           1     4            2   Painting
            84           8     0            0   Business & Administration
            88           8     1            0   Administration
            96           8     1            1   Public Administration 
           107           8     1            2   Local Managements
           110           8     2            0   Finance
           119           8     2            1   Accounting
           124           8     2            2   Marketing

我有另一张表,其中包含该学院的学生信息。

StudentID  OrganizationID
        1              12
        2              12
        3              24
        5              28
        6              35
        8              51
        9              66
       31              69
       34              96
       45              88
       57              96
       66             107
       69             110
       72              69
       74             124

我想得到每个单位的学生数。如果学生的组织是ChildUnit,则应将其添加到当前Unit。如果ChildUnit大于0,则应将相应的学生计数添加到同一Unit,例如PhysicsScience的孩子。然后Science学生计数应返回2.

我的目标数据表应如下所示

ParentUnit                    UnitName    StudentCount
------------------------------------------------------
Education                     Sports                 2
Education                     Mathmatics             1
Education                     Science                2
Education                     Arts                   4
Business & Administration     Administration         4
Business & Administration     Finance                2

我是以程序化方式完成的。有许多for和if循环。然后我开始考虑是否可以使用更智能的SQL查询来完成。

1 个答案:

答案 0 :(得分:2)

这看起来并不那么困难。您正在寻找每个ParentUnit +单元的学生计数。然后,这样一个组的名称是级别(ChildUnit)为零的记录。你用CASE结构得到那个记录,然后使用MIN或MAX,因为你需要一个聚合函数(无论如何每个组应该只有一个记录,所以MIN = MAX)。

select 
  min(case when o.childunit = 0 then o.unitname end) as unitname,
  count(*) as studentcount
from organization o
inner join student s on s.organizationid = o.organizationid
group by o.parentunit, o.unit;

要包含父单位名称:

select 
  (
    select unitname 
    from organization po 
    where po.parentunit = o.parentunit
    and po.unit =0
    and po.childunit = 0
  ) as parentunitname,
  min(case when o.childunit = 0 then o.unitname end) as unitname,
  count(*) as studentcount
from organization o
inner join student s on s.organizationid = o.organizationid
group by o.parentunit, o.unit;

或者:

select 
  min(po.unitname) as parentunitname,
  min(case when o.childunit = 0 then o.unitname end) as unitname,
  count(*) as studentcount
from organization o
inner join student s on s.organizationid = o.organizationid
inner join 
(
  select parentunit, unitname
  from organization 
  where unit = 0 and childunit = 0
) po on po.parentunit = o.parentunit
group by o.parentunit, o.unit;