由同一列分组的两个记录集之和之间的差异

时间:2015-05-22 11:51:34

标签: mysql sql oracle group-by aggregation

我需要编写一个SQL查询来计算欧洲和亚洲之间的收入差异。我受到app dev的约束,无法分两步计算(分别计算每个大陆的总数,然后计算差异)

这是db:

Sales(s_key, s_key, s_product, s_date, s_amount, s_quantity) 

Customer(c_key, c_name, c_city, c_country, c_continent)

您能否帮助我扩展这个计算欧洲收入以增加亚洲收入的查询? 谢谢!

Select Sum(s_amount)
From Sales
Where c_continent = 'Europe'
Group By c_continent

1 个答案:

答案 0 :(得分:4)

select  sum(case s_continent
            when 'Europe' then s_amount 
            when 'Asia' then -s_amount 
            end) as EuropeMinusAsia
from    Sales