加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

hive case when用法

(2015-04-13 23:15:49)
标签:

娱乐

group by是一个‘一行命令’
distinct 是一个‘多行命令’
做统计就需区分:分类项和指标项。


select pc.category_id,
sum(case when t.so_month between 3 and 5 then t.order_item_num else 0 end) as spring,
sum(case when t.so_month between 6 and 8 then t.order_item_num else 0 end) as summer,
sum(case when t.so_month between 9 and 11 then t.order_item_num else 0 end) as autumn,
sum(case when t.so_month=12 or t.so_month<=2 then t.order_item_num else 0 end) as winnter
from product_category pc join (select si.product_id, si.order_item_num, month(si.order_create_time) as so_month from so_item si where si.ds between '2013-05-01' and '2014-04-30' and si.is_gift=0) t on pc.product_id=t.product_id
group by pc.category_id;

把else后面的0改为0L即可

select pc.category_id,  
sum(case when t.so_month between 3 and 5 then t.order_item_num else 0L end) as spring,  
sum(case when t.so_month between 6 and 8 then t.order_item_num else 0L end) as summer,  
sum(case when t.so_month between 9 and 11 then t.order_item_num else 0L end) as autumn,  
sum(case when t.so_month=12 or t.so_month<=2 then t.order_item_num else 0L end) as winnter  
from product_category pc join (select si.product_id, si.order_item_num, month(si.order_create_time) as so_month from so_item si where si.ds between '2013-05-01' and '2014-04-30' and si.is_gift=0) t on pc.product_id=t.product_id  
group by pc.category_id


select number,
count(distinct(case when name='zhangsan' then '1' else name end) )as heratuv,
count((case when name='zhangsan' then '1' else name end) )as heratuv,
sum(case when age='20' then 1 else 0 end) as channeluv
from students group by number;

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有