1

我无法获得 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

4 回答 4

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
于 2013-04-05T17:10:15.943 回答
1

gl.building_name, gl.enrollmentGROUP 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;
于 2013-04-05T17:11:34.943 回答
0

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

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;

(对于绝大多数人来说, aLEFT JOIN比 a 直观得多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;
于 2013-04-05T17:42:23.890 回答
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
于 2013-04-05T17:51:02.693 回答