我無法獲得業務代碼相同的註冊總和。我的代碼如下:集團和總和
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
對不起SQL服務器 – Tone 2013-04-05 17:09:54
嘗試刪除入學率從您的集團聲明。 – zundarz 2013-04-05 17:13:34
爲什麼你有'DISTINCT' *和*'GROUP BY'?當你在WHERE子句中過濾外部表時,爲什麼一個'RIGHT JOIN',無論如何都使它成爲'INNER JOIN'? – 2013-04-05 17:38:30