Hive窗口函数之LAG、LEAD、FIRST_VALUE、LAST_VALUE的用法

一、创建表:

create table windows_ss

(

polno string,

eff_date string,

userno string

)

ROW FORMAT DELIMITED

FIELDS TERMINATED BY ‘,‘

stored as textfile;

数据准备:

P066666666666,2016-04-02 09:00:02,user01

P066666666666,2016-04-02 09:00:00,user02

P066666666666,2016-04-02 09:03:04,user11

P066666666666,2016-04-02 09:50:05,user03

P066666666666,2016-04-02 10:00:00,user51

P066666666666,2016-04-02 09:10:00,user09

P066666666666,2016-04-02 09:50:01,user32

P088888888888,2016-04-02 09:00:02,user41

P088888888888,2016-04-02 09:00:00,user55

P088888888888,2016-04-02 09:03:04,user23

P088888888888,2016-04-02 09:50:05,user80

P088888888888,2016-04-02 10:00:00,user08

P088888888888,2016-04-02 09:10:00,user22

P088888888888,2016-04-02 09:50:01,user31

将数据导入Hive表中:

LOAD DATA LOCAL INPATH  ‘/home/hadoop/testhivedata/windows_ss.txt‘  OVERWRITE INTO TABLE windows_ss;

LAG

LAG(col,n,DEFAULT) 用于统计窗口内往上第n行值
第一个参数为列名,第二个参数为往上第n行(可选,默认为1),第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)

SELECT

polno,

eff_date,

userno,

ROW_NUMBER() OVER(PARTITION BY polno ORDER BY eff_date) AS rn,

LAG(eff_date,1,‘1970-01-01 00:00:00‘) OVER(PARTITION BY polno ORDER BY eff_date) AS last_1_time,

LAG(eff_date,2) OVER(PARTITION BY polno ORDER BY eff_date) AS last_2_time

FROM windows_ss;

结果:

polno                        eff_date                              userno       rn    last_1_time                  last_2_time

P066666666666     2016-04-02 09:00:00      user02     1     1970-01-01 00:00:00      NULL

P066666666666     2016-04-02 09:00:02      user01     2     2016-04-02 09:00:00      NULL

P066666666666     2016-04-02 09:03:04      user11     3     2016-04-02 09:00:02      2016-04-02 09:00:00

P066666666666     2016-04-02 09:10:00      user09     4     2016-04-02 09:03:04      2016-04-02 09:00:02

P066666666666     2016-04-02 09:50:01      user32     5     2016-04-02 09:10:00      2016-04-02 09:03:04

P066666666666     2016-04-02 09:50:05      user03     6     2016-04-02 09:50:01      2016-04-02 09:10:00

P066666666666     2016-04-02 10:00:00      user51     7     2016-04-02 09:50:05      2016-04-02 09:50:01

P088888888888     2016-04-02 09:00:00      user55     1     1970-01-01 00:00:00      NULL

P088888888888     2016-04-02 09:00:02      user41     2     2016-04-02 09:00:00      NULL

P088888888888     2016-04-02 09:03:04      user23     3     2016-04-02 09:00:02      2016-04-02 09:00:00

P088888888888     2016-04-02 09:10:00      user22     4     2016-04-02 09:03:04      2016-04-02 09:00:02

P088888888888     2016-04-02 09:50:01      user31     5     2016-04-02 09:10:00      2016-04-02 09:03:04

P088888888888     2016-04-02 09:50:05      user80     6     2016-04-02 09:50:01      2016-04-02 09:10:00

P088888888888     2016-04-02 10:00:00      user08     7     2016-04-02 09:50:05      2016-04-02 09:50:01

分析:

last_1_time: 指定了往上第1行的值,default为‘1970-01-01 00:00:00‘

P066666666666第一行,往上1行为NULL,因此取默认值 1970-01-01 00:00:00

P066666666666第三行,往上1行值为第二行值,2016-04-02 09:00:02

P066666666666第六行,往上1行值为第五行值,2016-04-02 09:50:01

last_2_time: 指定了往上第2行的值,为指定默认值

P088888888888第一行,往上2行为NULL

P088888888888第二行,往上2行为NULL

P088888888888第四行,往上2行为第二行值,2016-04-02 09:00:02

P088888888888第七行,往上2行为第五行值,2016-04-02 09:50:01

LEAD

与LAG相反

LEAD(col,n,DEFAULT) 用于统计窗口内往下第n行值

第一个参数为列名,第二个参数为往下第n行(可选,默认为1),第三个参数为默认值(当往下第n行为NULL时候,取默认值,如不指定,则为NULL)

SELECT

polno,

eff_date,

userno,

ROW_NUMBER() OVER(PARTITION BY polno ORDER BY eff_date) AS rn,

LEAD(eff_date,1,‘1970-01-01 00:00:00‘) OVER(PARTITION BY polno ORDER BY eff_date) AS next_1_time,

LEAD(eff_date,2) OVER(PARTITION BY polno ORDER BY eff_date) AS next_2_time

FROM windows_ss;

结果:

polno                                  eff_date                     userno   rn    next_1_time                 next_2_time

P066666666666     2016-04-02 09:00:00      user02     1     2016-04-02 09:00:02      2016-04-02 09:03:04

P066666666666     2016-04-02 09:00:02      user01     2     2016-04-02 09:03:04      2016-04-02 09:10:00

P066666666666     2016-04-02 09:03:04      user11     3     2016-04-02 09:10:00      2016-04-02 09:50:01

P066666666666     2016-04-02 09:10:00      user09     4     2016-04-02 09:50:01      2016-04-02 09:50:05

P066666666666     2016-04-02 09:50:01      user32     5     2016-04-02 09:50:05      2016-04-02 10:00:00

P066666666666     2016-04-02 09:50:05      user03     6     2016-04-02 10:00:00      NULL

P066666666666     2016-04-02 10:00:00      user51     7     1970-01-01 00:00:00      NULL

P088888888888     2016-04-02 09:00:00      user55     1     2016-04-02 09:00:02      2016-04-02 09:03:04

P088888888888     2016-04-02 09:00:02      user41     2     2016-04-02 09:03:04      2016-04-02 09:10:00

P088888888888     2016-04-02 09:03:04      user23     3     2016-04-02 09:10:00      2016-04-02 09:50:01

P088888888888     2016-04-02 09:10:00      user22     4     2016-04-02 09:50:01      2016-04-02 09:50:05

P088888888888     2016-04-02 09:50:01      user31     5     2016-04-02 09:50:05      2016-04-02 10:00:00

P088888888888     2016-04-02 09:50:05      user80     6     2016-04-02 10:00:00      NULL

P088888888888     2016-04-02 10:00:00      user08     7     1970-01-01 00:00:00      NULL

分析:

--逻辑与LAG一样,只不过LAG是往上,LEAD是往下

FIRST_VALUE

取分组内排序后,截止到当前行,第一个值

SELECT

polno,

eff_date,

userno,

ROW_NUMBER() OVER(PARTITION BY polno ORDER BY eff_date) AS rn,

FIRST_VALUE(userno) OVER(PARTITION BY polno ORDER BY eff_date) AS first1

FROM windows_ss;

polno                  eff_date                  userno     rn    first1

P066666666666     2016-04-02 09:00:00      user02     1     user02

P066666666666     2016-04-02 09:00:02      user01     2     user02

P066666666666     2016-04-02 09:03:04      user11     3     user02

P066666666666     2016-04-02 09:10:00      user09     4     user02

P066666666666     2016-04-02 09:50:01      user32     5     user02

P066666666666     2016-04-02 09:50:05      user03     6     user02

P066666666666     2016-04-02 10:00:00      user51     7     user02

P088888888888     2016-04-02 09:00:00      user55     1     user55

P088888888888     2016-04-02 09:00:02      user41     2     user55

P088888888888     2016-04-02 09:03:04      user23     3     user55

P088888888888     2016-04-02 09:10:00      user22     4     user55

P088888888888     2016-04-02 09:50:01      user31     5     user55

P088888888888     2016-04-02 09:50:05      user80     6     user55

P088888888888     2016-04-02 10:00:00      user08     7     user55

LAST_VALUE

取分组内排序后,截止到当前行,最后一个值

SELECT

polno,

eff_date,

userno,

ROW_NUMBER() OVER(PARTITION BY polno ORDER BY eff_date) AS rn,

LAST_VALUE(userno) OVER(PARTITION BY polno ORDER BY eff_date) AS last1

FROM windows_ss;

结果:

polno                                  eff_date                   userno      rn    last1

P066666666666     2016-04-02 09:00:00      user02     1     user02

P066666666666     2016-04-02 09:00:02      user01     2     user01

P066666666666     2016-04-02 09:03:04      user11     3     user11

P066666666666     2016-04-02 09:10:00      user09     4     user09

P066666666666     2016-04-02 09:50:01      user32     5     user32

P066666666666     2016-04-02 09:50:05      user03     6     user03

P066666666666     2016-04-02 10:00:00      user51     7     user51

P088888888888     2016-04-02 09:00:00      user55     1     user55

P088888888888     2016-04-02 09:00:02      user41     2     user41

P088888888888     2016-04-02 09:03:04      user23     3     user23

P088888888888     2016-04-02 09:10:00      user22     4     user22

P088888888888     2016-04-02 09:50:01      user31     5     user31

P088888888888     2016-04-02 09:50:05      user80     6     user80

P088888888888     2016-04-02 10:00:00      user08     7     user08

如果不指定ORDER BY,则默认按照记录在文件中的偏移量进行排序,会出现错误的结果

FIRST_VALUE没有排序:

SELECT

polno,

eff_date,

userno,

FIRST_VALUE(userno) OVER(PARTITION BY polno) AS first2

FROM windows_ss;

polno                             eff_date                          userno   first2

P066666666666     2016-04-02 09:00:02      user01     user01

P066666666666     2016-04-02 09:00:00      user02     user01

P066666666666     2016-04-02 09:03:04      user11     user01

P066666666666     2016-04-02 09:50:05      user03     user01

P066666666666     2016-04-02 10:00:00      user51     user01

P066666666666     2016-04-02 09:10:00      user09     user01

P066666666666     2016-04-02 09:50:01      user32     user01

P088888888888     2016-04-02 09:00:02      user41     user41

P088888888888     2016-04-02 09:00:00      user55     user41

P088888888888     2016-04-02 09:03:04      user23     user41

P088888888888     2016-04-02 09:50:05      user80     user41

P088888888888     2016-04-02 10:00:00      user08     user41

P088888888888     2016-04-02 09:10:00      user22     user41

P088888888888     2016-04-02 09:50:01      user31     user41

LAST_VALUE没有排序:

SELECT

polno,

eff_date,

userno,

LAST_VALUE(userno) OVER(PARTITION BY polno) AS last2

FROM windows_ss;

结果:

polno                           eff_date                              userno last2

P066666666666     2016-04-02 09:00:02      user01     user32

P066666666666     2016-04-02 09:00:00      user02     user32

P066666666666     2016-04-02 09:03:04      user11     user32

P066666666666     2016-04-02 09:50:05      user03     user32

P066666666666     2016-04-02 10:00:00      user51     user32

P066666666666     2016-04-02 09:10:00      user09     user32

P066666666666     2016-04-02 09:50:01      user32     user32

P088888888888     2016-04-02 09:00:02      user41     user31

P088888888888     2016-04-02 09:00:00      user55     user31

P088888888888     2016-04-02 09:03:04      user23     user31

P088888888888     2016-04-02 09:50:05      user80     user31

P088888888888     2016-04-02 10:00:00      user08     user31

P088888888888     2016-04-02 09:10:00      user22     user31

P088888888888     2016-04-02 09:50:01      user31     user31
如果想要取分组内排序后最后一个值,则需要变通一下:

SELECT

polno,

eff_date,

userno,

ROW_NUMBER() OVER(PARTITION BY polno ORDER BY eff_date) AS rn,

LAST_VALUE(userno) OVER(PARTITION BY polno ORDER BY eff_date) AS last1,

FIRST_VALUE(userno) OVER(PARTITION BY polno ORDER BY eff_date DESC) AS last2

FROM windows_ss ORDER BY polno,eff_date;

polno                                 eff_date                     userno     rn    last1       last2

P066666666666     2016-04-02 09:00:00      user02     1     user02     user51

P066666666666     2016-04-02 09:00:02      user01     2     user01     user51

P066666666666     2016-04-02 09:03:04      user11     3     user11     user51

P066666666666     2016-04-02 09:10:00      user09     4     user09     user51

P066666666666     2016-04-02 09:50:01      user32     5     user32     user51

P066666666666     2016-04-02 09:50:05      user03     6     user03     user51

P066666666666     2016-04-02 10:00:00      user51     7     user51     user51

P088888888888     2016-04-02 09:00:00      user55     1     user55     user08

P088888888888     2016-04-02 09:00:02      user41     2     user41     user08

P088888888888     2016-04-02 09:03:04      user23     3     user23     user08

P088888888888     2016-04-02 09:10:00      user22     4     user22     user08

P088888888888     2016-04-02 09:50:01      user31     5     user31     user08

P088888888888     2016-04-02 09:50:05      user80     6     user80     user08

P088888888888     2016-04-02 10:00:00      user08     7     user08     user08

注意:

在使用分析函数的过程中,要特别注意ORDERBY子句,用的不恰当,统计出的结果就不是你所期望的

原文地址:https://www.cnblogs.com/zmoumou/p/10222127.html

时间: 2024-10-11 14:44:27

Hive窗口函数之LAG、LEAD、FIRST_VALUE、LAST_VALUE的用法的相关文章

Hive分析窗口函数(四) LAG,LEAD,FIRST_VALUE,LAST_VALUE

1.LAG功能是什么? 2.LEAD与LAG功能有什么相似的地方那个? 3.FIRST_VALUE与LAST_VALUE分别完成什么功能? 继续学习这四个分析函数. 注意: 这几个函数不支持WINDOW子句. Hive版本为 apache-hive-0.13.1 数据准备: 水电费 cookie1,2015-04-10 10:00:02,url2 cookie1,2015-04-10 10:00:00,url1 cookie1,2015-04-10 10:03:04,1url3 cookie1,

Hive函数:LAG,LEAD,FIRST_VALUE,LAST_VALUE

参考自大数据田地:http://lxw1234.com/archives/2015/04/190.htm 测试数据准备: create external table test_data ( cookieid string, createtime string, --页面访问时间 url string --被访问页面 ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' stored as textfile location '/user/jc_rc_ft

Hive窗口函数

参考地址:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+WindowingAndAnalytics 环境准备: CREATE TABLE `wtab`( `a` int, `b` int, `c` int); 数据: 1.LEAD函数 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-colo

Oracle lag()/lead() over()分析函数

with tmp as(select '1' id ,'aa' name ,'22' age from dual union allselect '2' id ,'bb' name ,'20' age from dual union allselect '3' id ,'CC' name ,'21' age from dual)select a.*, lead(age,1) over (order by id desc) lag, a.age - lead(age,1) over (order

over 分析函数之 lag() lead()

/*语法*/ lag(exp_str,offset,defval) over()  取前 Lead(exp_str,offset,defval) over()  取后 --exp_str要取的列 --offset取偏移后的第几行数据 --defval:没有符合条件的默认值 eg1: with tmp as(select '1' id ,'aa' name from dual union allselect '2' id ,'bb' name from dualunion allselect '3

oracle 分析函数lag lead

LAG is an analytic function. It provides access to more than one row of a table at the same time without a self join. Given a series of rows returned from a query and a position of the cursor, LAG provides access to a row at a given physical offset p

Hive窗口函数最全案例详解

语法: 分析函数 over(partition by 列名 order by 列名 rows between 开始位置 and 结束位置) 常用分析函数: 聚合类 avg().sum().max().min() 排名类 row_number() 按照值排序时产生一个自增编号,不会重复 rank() 按照值排序时产生一个自增编号,值相等时会重复,会产生空位 dense_rank() 按照值排序时产生一个自增编号,值相等时会重复,不会产生空位 其他类 lag(列名,往前的行数,[行数为null时的默

Hive分析窗口函数

Hive分析窗口函数(四) LAG,LEAD,FIRST_VALUE,LAST_VALUE https://cwiki.apache.org/confluence/display/Hive/LanguageManual+WindowingAndAnalytics

9重点第九章开窗函数与表旋转(转载)

9重点第九章开窗函数与表旋转 小结: 由于开窗函数的起点是基础查询的结果集合,并且基础查询的结果集合只有到 SELECT 阶段时才生成,因此,开窗函数仅能用于查询的 SELECT 和ORDER BY 子句. 1. 开窗函数包括:排名开窗函数(ROW_NUMBER(), RANK()等),聚合开窗函数如(AVG,SUM等).在 SQLSERVER 2012 中,还提供了4个偏移开窗函数: LAG, LEAD , FIRST_VALUE和LAST_VALUE 此外,2012 开始支持了 对聚合的排序