case when用法

原创 2016年08月31日 16:30:54

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



相关文章推荐

oracle case when 用法总结 (转)

ORACLE CASE WHEN 及 SELECT CASE WHEN的用法   Case具有两种格式。简单Case函数和Case搜索函数。 Sql代码   ...

Oracle中CASE WHEN的用法实例

实例演示:   (1)查询表users中的数据。   select u.id,u.realname,U.SEX from users u;   查询结果如下   ID    REALNAME ...
  • jkaies
  • jkaies
  • 2013年08月08日 13:53
  • 352

SQL中Case When 用法举例

Case具有两种格式。简单Case函数和Case搜索函数。 --简单Case函数 CASE sex  WHEN '1' THEN '男'  WHEN '2' THEN '女'  ELSE '其他' E...

CASE WHEN 用法介绍2

http://www.cnblogs.com/eshizhan/archive/2012/04/06/2435493.html Oracle CASE WHEN 用法介绍 1. C...

SQL Server---Case When用法简介

上一篇博客使用了casewhen解决了数据汇总的问题,那么这一篇博客我将简单的介绍一下关于case when的使用。...

Oracle中的case when then 用法.

case when then 多条件判断 博客分类: sql SQL CASE     WHEN 条件1 THEN 结果1     WHEN 条件2 THEN 结果2     ...

case when和decode的用法与区别

一、case when case when 类似我们的if ...else ,判断语句 语法如下: CASE expr WHEN expr1 THEN return_expr1        ...

Oracle CASE WHEN 用法介绍

Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' ...

SQL中的CASE WHEN用法 .

SQL中Case的使用方法     Case具有两种格式。简单Case函数和Case搜索函数。    --简单Case函数     CASE sex              WHEN '1' THE...
  • lchwhy
  • lchwhy
  • 2011年11月13日 10:23
  • 218

SQL__case_when_then_的用法

  • 2013年11月21日 16:46
  • 24KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:case when用法
举报原因:
原因补充:

(最多只允许输入30个字)