case when用法

1、划分时间段

select deviceid,
case when server_time >=20160825000000 and server_time <20160825010000 then 1
when server_time >=20160825010000 and server_time <20160825020000 then 2
when server_time >=20160825020000 and server_time <20160825030000 then 3
when server_time >=20160825030000 and server_time <20160825040000 then 4
when server_time >=20160825040000 and server_time <20160825050000 then 5
when server_time >=20160825050000 and server_time <20160825060000 then 6
when server_time >=20160825060000 and server_time <20160825070000 then 7
when server_time >=20160825070000 and server_time <20160825080000 then 8
when server_time >=20160825080000 and server_time <20160825090000 then 9
when server_time >=20160825090000 and server_time <20160825100000 then 10
when server_time >=20160825100000 and server_time <20160825110000 then 11
when server_time >=20160825110000 and server_time <20160825120000 then 12
when server_time >=20160825120000 and server_time <20160825130000 then 13
when server_time >=20160825130000 and server_time <20160825140000 then 14
when server_time >=20160825140000 and server_time <20160825150000 then 15
when server_time >=20160825150000 and server_time <20160825160000 then 16
when server_time >=20160825160000 and server_time <20160825170000 then 17
when server_time >=20160825170000 and server_time <20160825180000 then 18
when server_time >=20160825180000 and server_time <20160825190000 then 19
when server_time >=20160825190000 and server_time <20160825200000 then 20
when server_time >=20160825200000 and server_time <20160825210000 then 21
when server_time >=20160825210000 and server_time <20160825220000 then 22
when server_time >=20160825220000 and server_time <20160825230000 then 23
when server_time >=20160825230000 and server_time <20160826000000 then 24
end timerange
from base.transfer_dis

2、统计数据量

select type,count(distinct deviceid) num
from(
select deviceid,
case when title like '%.apk' then "apk"
when title like '%.mp4' then "mp4"
when title like '%.mp3' then "mp3"
when title like '%.jpg' then "jpg"
when title like '%.m4a' then "m4a"
when title like '%.gif' then "gif"
when title like '%.aac' then "aac"
when title like '%.jpg' then "jpg"
end type
from base.transfer_dis
where concat(year,month,day) = 20160825
and country = "中国"
and server_time >= 20160825200000
and server_time <= 20160825230000) tmp
group by type


3、直接统计数据量

set mapred.job.priority=very_high;
set mapred.reduce.tasks=40;
select count(distinct case when title like '%.apk' then deviceid end) apk,
 count(distinct case when title like '%.mp4' then deviceid end) mp4,
 count(distinct case when title like '%.mp3' then deviceid end) mp3,
 count(distinct case when title like '%.jpg' then deviceid end) jpg,
 count(distinct case when title like '%.m4a' then deviceid end) m4a,
 count(distinct case when title like '%.gif' then deviceid end) gif,
 count(distinct case when title like '%.aac' then deviceid end) aac,
 count(distinct case when title like '%.jpg' then deviceid end) jpg
from base.transfer_dis
where concat(year,month,day) = 20160825
and country = "中国"
and server_time >= 20160825200000
and server_time <= 20160825230000



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值