码迷,mamicode.com
首页 > 数据库 > 详细

SQL技巧之分类汇总

时间:2016-06-14 22:29:14      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

数据表结构
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

 

SQL技巧之分类汇总

标签:

原文地址:http://www.cnblogs.com/guwei4037/p/5585473.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!