-- LEAD(col,n,DEFAULT) 用于统计窗口内往下第n行值
-- 第一个参数为列名,第二个参数为往下第n行(可选,默认为1),第三个参数为默认值(当往下第n行为NULL时候,取默认值,如不指定,则为NULL)
-- LAG(col,n,DEFAULT) 用于统计窗口内往上第n行值
-- 第一个参数为列名,第二个参数为往上第n行(可选,默认为1),第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)
-- FIRST_VALUE 取分组内排序后,截止到当前行,第一个值
-- LAST_VALUE 取分组内排序后,截止到当前行,最后一个值
-- 这几个函数不支持WINDOW子句 select
t2.id
,t2.day
,t2.lead_default_day
,t2.lead_2_day
,t2.lag_default_day
,t2.lag_2_day
,t2.first_day_1
,t2.first_day_2
,t2.last_day_1
,t2.last_day_2
,(unix_timestamp(t2.lead_default_day)-unix_timestamp(t2.day))/3600 as diff_hour
from (
select
t1.id
,t1.day
,lead(t1.day) over(partition by t1.id order by t1.day) as lead_default_day
,lead(t1.day,1,'2018-01-01 00:00:00') over(partition by t1.id order by t1.day) as lead_2_day
,lag(t1.day) over(partition by t1.id order by t1.day) as lag_default_day
,lag(t1.day,1,'2018-01-01 00:00:00') over(partition by t1.id order by t1.day) as lag_2_day
,first_value(t1.day) over(partition by t1.id order by t1.day) as first_day_1
,first_value(t1.day) over(partition by t1.id) as first_day_2
,last_value(t1.day) over(partition by t1.id order by t1.day) as last_day_1
,last_value(t1.day) over(partition by t1.id) as last_day_2
from (
select 'a' as id, '2018-01-01 12:22:00' as day union all
select 'a' as id, '2018-01-09 00:00:00' as day union all
select 'a' as id, '2018-01-02 00:00:00' as day union all
select 'a' as id, '2018-01-03 00:00:00' as day union all
select 'a' as id, '2018-01-04 00:00:00' as day union all
select 'b' as id, '2018-01-08 00:00:00' as day union all
select 'b' as id, '2018-01-05 00:00:00' as day union all
select 'b' as id, '2018-01-06 00:00:00' as day union all
select 'b' as id, '2018-01-07 00:00:00' as day
) t1
) t2
;
+-----+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+---------------------+--+
| id | day | lead_default_day | lead_2_day | lag_2_day | first_day_1 | first_day_2 | last_day_1 | last_day_2 | diff_hour |
+-----+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+---------------------+--+
| b | 2018-01-05 00:00:00 | 2018-01-06 00:00:00 | 2018-01-06 00:00:00 | 2018-01-01 00:00:00 | 2018-01-05 00:00:00 | 2018-01-05 00:00:00 | 2018-01-05 00:00:00 | 2018-01-08 00:00:00 | 24.0 |
| b | 2018-01-06 00:00:00 | 2018-01-07 00:00:00 | 2018-01-07 00:00:00 | 2018-01-05 00:00:00 | 2018-01-05 00:00:00 | 2018-01-05 00:00:00 | 2018-01-06 00:00:00 | 2018-01-08 00:00:00 | 24.0 |
| b | 2018-01-07 00:00:00 | 2018-01-08 00:00:00 | 2018-01-08 00:00:00 | 2018-01-06 00:00:00 | 2018-01-05 00:00:00 | 2018-01-05 00:00:00 | 2018-01-07 00:00:00 | 2018-01-08 00:00:00 | 24.0 |
| b | 2018-01-08 00:00:00 | NULL | 2018-01-01 00:00:00 | 2018-01-07 00:00:00 | 2018-01-05 00:00:00 | 2018-01-05 00:00:00 | 2018-01-08 00:00:00 | 2018-01-08 00:00:00 | NULL |
| a | 2018-01-01 12:22:00 | 2018-01-02 00:00:00 | 2018-01-02 00:00:00 | 2018-01-01 00:00:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-09 00:00:00 | 11.633333333333333 |
| a | 2018-01-02 00:00:00 | 2018-01-03 00:00:00 | 2018-01-03 00:00:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-02 00:00:00 | 2018-01-09 00:00:00 | 24.0 |
| a | 2018-01-03 00:00:00 | 2018-01-04 00:00:00 | 2018-01-04 00:00:00 | 2018-01-02 00:00:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-03 00:00:00 | 2018-01-09 00:00:00 | 24.0 |
| a | 2018-01-04 00:00:00 | 2018-01-09 00:00:00 | 2018-01-09 00:00:00 | 2018-01-03 00:00:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-04 00:00:00 | 2018-01-09 00:00:00 | 120.0 |
| a | 2018-01-09 00:00:00 | NULL | 2018-01-01 00:00:00 | 2018-01-04 00:00:00 | 2018-01-01 12:22:00 | 2018-01-01 12:22:00 | 2018-01-09 00:00:00 | 2018-01-09 00:00:00 | NULL |
+-----+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+----------------------+---------------------+--+

hive 取两次记录的时间差 lead lag first_value last_value的更多相关文章

  1. Hive 窗口函数LEAD LAG FIRST_VALUE LAST_VALUE

    窗口函数(window functions)对多行进行操作,并为查询中的每一行返回一个值. OVER()子句能将窗口函数与其他分析函数(analytical functions)和报告函数(repor ...

  2. SQLServer 分组查询相邻两条记录的时间差

    原文:SQLServer 分组查询相邻两条记录的时间差 首先,我们通过数据库中表的两条记录来引出问题,如下图 以上为一个记录操作记录的表数据.OrderID为自增长列,后面依次为操作类型,操作时间,操 ...

  3. hive实现根据用户分组,按用户记录求上下两条记录的时间差

    在mysql,数据如下:#查询某一用户该日抽奖时间 select draw_time from user_draw_log where user_id = 1 and draw_date='2016- ...

  4. sql查询两条记录的时间差

    今天突然想到了一个需求,即在一张带有id和time字段的表中,查询相邻时间的时间差. 表的记录如下: 表名为wangxin id是一个不重复的字符串,time是一个时间戳. 现在的需求如下: 比如id ...

  5. Gym 101064 D Black Hills golden jewels 【二分套二分/给定一个序列,从序列中任意取两个数形成一个和,两个数不可相同,要求求出第k小的组合】

    D. Black Hills golden jewels time limit per test 2 seconds memory limit per test 256 megabytes input ...

  6. 【转】oracle 中随机取一条记录的两种方法

    oracle 中随机取一条记录的两种方法 V_COUNT INT:=0; V_NUM INT :=0; 1:TBL_MYTABLE 表中要有一个值连续且唯一的列FID BEGIN SELECT COU ...

  7. Oracle 取两个表中数据的交集并集差异集合

    Oracle 取两个表中数据的交集 关键字: Oracle 取两个表中数据的交集 INTERSECT Oracle 作为一个大型的关系数据库,日常应用中往往需要提取两个表的交集数据 例如现有如下表,要 ...

  8. C#两个时间的时间差的方法

    今天遇到一问题,计算两个时间的时间差,看网上的写法较为复杂,找到个简单点的,记录下作为自己的总结. 关键函数: DateTime.Subtract 函数解释: 从此实例中减去指定的日期和时间,返回一个 ...

  9. hive取数时如果遇到这种报错

    如果你hive取数时遇到这种报错:ParseException line 1:78 cannot recognize input near '<EOF>' '<EOF>' '& ...

随机推荐

  1. 1-1+zookeeper简介

     zookeeper是中间件,可以为分布式系统提供协调服务.如果没有分布式系统,zookeeper也发挥不了它的优势.

  2. bluebird 开发文档链接

    参考文献:http://bluebirdjs.com/docs/api/promise.mapseries.html

  3. cocos2d-js 骨骼动画 3.10

    近期使用了cocos动画中的骨骼动画,这里记录使用的两种方式(3.10版): 一.cocos自带的动画编辑器导出的动画 ccs.armatureDataManager.addArmatureFileI ...

  4. Opengl使用模型视图变换移动光源

    光源绕一个物体旋转,按下鼠标左键时,光源位置旋转. #include <GL/glut.h> static int spin = 0;static GLdouble x_1 = 0.0;s ...

  5. windows xp版本的chrome浏览器去哪里下载呢?

    http://www.265.com/chrome-download/?slice 265没记错的话应该是GOOGLE的一个导航网站吧,所以可信度是比较高的.

  6. R: 主成分分析 ~ PCA(Principal Component Analysis)

    本文摘自:http://www.cnblogs.com/longzhongren/p/4300593.html 以表感谢. 综述: 主成分分析 因子分析 典型相关分析,三种方法的共同点主要是用来对数据 ...

  7. 数据结构 i_love(我喜欢)

    数据结构 i_love(我喜欢) 问题描述 集训队的学长们都怪怪的,如果 A 学长喜欢 B 学长, A 就会把自己的名字改成«I_love_<B 学长的名字>».但是奇怪的学长们很容易移情 ...

  8. HUST高级软件工程--测试管理工具实践--Day4

    测试管理工具实践--Day4 今天完成任务情况: 小靳 今天,主要在前两天的基础上继续学习挖掘jira相关内容: 学会了如何创建项目,并且创建了issue 学会了创建一般账号,并且可以将任务分发给一般 ...

  9. ISE 14.7 XST.exe stop working

    http://www.xilinx.com/support/answers/59851.html Description XST completes but then instead of retur ...

  10. undefined reference to `clock_gettime

    下面这个错误通常是因为链接选项里漏了-lrt,但有时发现即使加了-lrt仍出现这个问题,使用nm命令一直,会发现-lrt最终指向的文件没有包含任何symbol,这个时候,可以找相应的静态库版本libr ...