分组和总和

时间:2013-04-05 17:07:52

标签: sql-server group-by aggregate-functions

我无法获得business_code相同的注册总和。我的代码如下:

SELECT DISTINCT lb.building_code ,  lb.bus_code, gl.building_name, gl.bus_name, SUM(gl.enrollment) AS enrollment 
  FROM table1 AS gl 
  RIGHT OUTER JOIN table 2 AS lb ON gl.building_key = lb.building_key
  where gl.bus_name = 'Business'
  and gl.year_cd = 2010
  GROUP BY lb.building_code,  lb.bus_code, gl.building_name, gl.bus_name, gl.enrollment

当前输出:

building_code   bus_code    bus_name      enrollment  
4581             0000       Business A    12
4581             0000       Business A    13
4581             0109       Business B    100
4581             0109       Business B    120 
4581             0209       Business C    130 
4581             0402       Business D    35 

期望的输出:

 building_code   bus_code    bus_name      enrollment  
    4581             0000       Business A    25
    4581             0109       Business B    220
    4581             0209       Business C    130 
    4581             0402       Business D    35 

4 个答案:

答案 0 :(得分:1)

  SELECT lb.building_code,  
         lb.bus_code, 
         gl.bus_name,       
         SUM(gl.enrollment) AS enrollment 
  FROM table1 AS gl 
  RIGHT OUTER JOIN table 2 AS lb ON gl.building_key = lb.building_key
  where gl.bus_name = 'Business'
  and gl.year_cd = 2010
  GROUP BY lb.building_code, 
           lb.bus_code, 
           gl.bus_name

答案 1 :(得分:1)

gl.building_name, gl.enrollment子句中删除GROUP BY

SELECT 
  lb.building_code ,  
  lb.bus_code, 
  gl.bus_name, 
  SUM(gl.enrollment) AS enrollment 
FROM table1 AS gl 
RIGHT OUTER JOIN table 2 AS lb ON gl.building_key = lb.building_key
where gl.bus_name = 'Business'
  and gl.year_cd = 2010
GROUP BY lb.building_code,  lb.bus_code, gl.bus_name;

答案 2 :(得分:0)

我会考虑两次重写。一,如果你认为这是一个外连接(所以包括table2中不在table1中的行),更改顺序,使其成为左连接,将table1的where子句移动到join子句中,删除distinct,并通过以下方式从组中删除未分组的列:

SELECT lb.building_code, lb.bus_code, gl.building_name, 
  gl.bus_name, SUM(gl.enrollment) AS enrollment 
FROM dbo.table2 AS lb
LEFT OUTER JOIN dbo.table1 AS gl 
  ON gl.building_key = lb.building_key
  AND gl.bus_name = 'Business'
  AND gl.year_cd = 2010
GROUP BY lb.building_code, lb.bus_code, gl.building_name, gl.bus_name;

(对于绝大多数人来说,LEFT JOIN远比RIGHT JOIN更直观。)

如果你真的不希望table2中有任何不在table1中的行,那么首先不要把它写成外连接:

SELECT lb.building_code, lb.bus_code, gl.building_name, 
  gl.bus_name, SUM(gl.enrollment) AS enrollment 
FROM dbo.table2 AS lb
INNER JOIN dbo.table1 AS gl 
  ON gl.building_key = lb.building_key
WHERE gl.bus_name = 'Business'
  AND gl.year_cd = 2010
GROUP BY lb.building_code, lb.bus_code, gl.building_name, gl.bus_name;

答案 3 :(得分:0)

尝试

由于您正在使用右外连接,因此不要忘记在SUM聚合器函数中添加IsNull来处理来自表2的不匹配数据

SELECT lb.building_code ,  lb.bus_code, gl.bus_name, SUM(Isnull(gl.enrollment,0)) AS enrollment 
  FROM table1 AS gl 
  RIGHT OUTER JOIN table 2 AS lb ON gl.building_key = lb.building_key
  where gl.bus_name = 'Business'
  and gl.year_cd = 2010
  GROUP BY lb.building_code ,  lb.bus_code, gl.bus_name