Sql 如何求同一单元的和

Sql 如何求同一单元的和,sql,sql-server,Sql,Sql Server,我有一张表格,里面有一所学院的系及其单位和子单位 OrganizationID ParentUnit Unit ChildUnit UnitName 10 1 0 0 Education 12 1 1 0 Sports 24 1 2 0 Mathmat

我有一张表格,里面有一所学院的系及其单位和子单位

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大于0,则应将相应的学生人数添加到相同的单位,例如,物理学是科学的子学科。然后理科学生计数应该返回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查询来完成它。

这看起来并不难。您正在查找每个家长单位+单位的学生人数。然后,这样一个组的名称是记录,其中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;
或:


我可以请求获得第三列作为ParentUnit的名称吗?ParentUnit,Unit,StudentCount是否有一个ParentUnits表,用于保存每个ParentUnit的名称?然后:从parentunits p中选择select name,其中p.parentunit=o.parentunit作为parentunit,mincase。。。或者连接该表,并在名称上使用MIN或MAX,或者在GROUP BY子句中使用have name。否,相同表中的所有单位。ParentUnit、Unit和ChildUnit列标识哪个单元在哪个单元下。啊,我想我现在明白了。ParentUnit名称是Unit=0和ChildUnit=0的名称,是吗?我会相应地更新我的答案。是的,正如你所说的。当o.childunit=0时,我尝试过mincase,然后o.unitname以unitname结尾,count*以studentcount结尾,当o.unit=0时尝试过mincase,然后o.unitname以parentunitname结尾。。。但是parentunitname对于所有行都为空。
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;