hive经典应用

dual的构造

自己构造即可一个函数几个,在随后的select 测试 from dual;

前后两行求时间差

1.Hive row_number() 函数的高级用法 row_num 按照某个字段分区显示第几条数据

select imei,ts,fuel_instant,gps_longitude,gps_latitude,row_number() over (PARTITION BY imei ORDER BY ts ASC) as row_num from sample_data_2

2.row_num 是相互连续的,join 自身,然后时间相减可求差
create table obd_20140101 as

  select a.imei,a.row_num,a.ts,COALESCE(unix_timestamp(a.ts, ‘yyyy-MM-dd HH:mm:ss.S‘), 0) - unix_timestamp(b.ts, ‘yyyy-MM-dd HH:mm:ss.S‘) as intervel ,a.fuel_instant,a.gps_speed as obd_speed,a.gps_status,a.gps_longitude,a.gps_latitude,a.direct_angle,a.obdspeed from obddata_20140101 a join obddata_20140101 b on a.imei = b.imei and a.row_num = b.row_num +1

分组排序求每个类别的TOP10

语法:row_number() over (partition by 字段a order by 计算项b desc ) rank 

--这里rank是别名


partition by:类似hive的建表,分区的意思;

order by :排序,默认是升序,加desc降序;

这里按字段a分区,对计算项b进行降序排序


实例:

要取top10品牌,各品牌的top10渠道,各品牌的top10渠道中各渠道的top10档期 

1、取top10品牌

select 品牌,count/sum/其它() as num  from table_name order by num limit 10;


 2、 取top10品牌下各品牌的top10渠道        

select 

          a.*

from

          (

                  select 品牌,渠道,count/sum/其它() as num row_number() over (partition by 品牌 order by num desc ) rank  

                   from table_name

                   where 品牌限制条件

                   group by 品牌,渠道

          )a

where 

          a.rank<=10

 3、 取top10品牌下各品牌的top10渠道中各渠道的top10档期

 

select 

          a.*

from

          (

                  select 品牌,渠道,档期,count/sum/其它() as num row_number() over (partition by 品牌,渠道 order by num desc ) rank  

                   from table_name

                   where 品牌,渠道 限制条件

                   group by 品牌,渠道,档期

          )a

where 

          a.rank<=10

待续


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值