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

待续


你可能感兴趣的:(hive经典应用)