使用SQL计算另一个表的总和

时间:2014-08-30 06:13:03

标签: sql

我有一个表(区)有列(Id - 区域名称)。另一个表(交付)有列(数量 - 区域ID)。我可以得到一个结果,如一个表中列中的每个地区名称和使用sql的其他列中的数量总和?

3 个答案:

答案 0 :(得分:0)

我知道这个问题将被关闭,因为这个网站不是要做功课

select a.district_name, b.total
from District as a
inner join
(
select district_id, sum(quantity) as total
from delivery
group by district_id
) as b
on a.id = b.district_id

答案 1 :(得分:0)

尝试以下代码

SELECT dis.district_name,SUM(del.quantity) as quantity
FROM district as dis 
INNER JOIN delivery as del 
ON dis.id = del.district_id
GROUP BY del.district_id

答案 2 :(得分:0)

您可以使用此代码:

SELECT ID, Name, SUM(Quantity) AS SumOfQuantity      
FROM  
(  
   SELECT District.ID, District.Name, Delivery.Quantity  
   FROM District, Delivery  
   WHERE District.Id = Delivery.DistrictID  
) AS T1  
GROUP BY ID, Name