Loading

SQL技巧之分类汇总

数据表结构
username type num
aaaa   玉米 1212
aaaa   玉米  212
bbb     小麦  2323
bbb ....

只有两种产品 玉米和小麦,玉米价格1.5,小麦价格1.2 求总价值?

 

with a as(
select type,sum(num) as num from foods group by type),

b as(
select type,(case when type='小麦' then 1.2 when type='玉米' then 1.5 end)*num as price  from a)

select sum(price) as result from b

 

posted @ 2016-06-14 21:05  guwei4037  阅读(1652)  评论(0编辑  收藏  举报