-- 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. PCL—点云滤波(基于点云频率) 低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5010771.html 1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概 ...

  2. 转:c语言学习笔记 二进制和十进制的互相转化

    http://www.cnblogs.com/xkfz007/articles/2590472.html

  3. Luogu 3943 星空

    原题是CF79D Password 很妙的题. 首先我们发现区间操作不太好弄,我们想办法把它转化成单点操作,这样子处理的办法会多一点. 方法当然是差分了. 定义差分数组$b_i = a_i \^ a_ ...

  4. 读取txt文件将文本行组合成特定格式

    有一网友要求从txt文本文件读取一些数据,然后组合为特定格式的数据行.原论题如下,刚才开始的要求描述得不太清楚,后来补充完整了. Insus.NET觉得本论题可有练习文本件读取功力,因此尝试实现一下. ...

  5. Android之ContextMenu的使用方法以及与OptionMenu的区别(转)

    >> ContextMenu是android的context menu上下文菜单,选择某项VIEW后长按menu键,就会显示出来.比如EditeText就可以通过长按来弹出拥有“cut”, ...

  6. STL 堆的使用

    本来是要写leetcode上的merge k sorted lists那道题目,这个题目我还是很熟悉的,毕竟是看过算法导论的人,但是写的过程中对堆的维护代码还是挺多的,所以我想到了STL中的堆.下面就 ...

  7. SQL SERVER 提供了一些时间函数:

    SQL SERVER 提供了一些时间函数:取当前时间:select getdate()取前一个月的时间:SELECT DATEADD(MONTH,-1,GETDATE()) 月份减一个月取年份:SEL ...

  8. TigerVNC编译安装

    TigerVNC official site:http://www.linuxfromscratch.org/blfs/view/svn/xsoft/tigervnc.html TigerVNC版本: ...

  9. Oracle SQL判断字符串是否在目标字符串中的函数

    转自:http://dacoolbaby.iteye.com/blog/1772156 根据需求,写了一段方法. 用于识别以下的情况: 判断 字符串A  在用逗号分隔的字符串B中是否存在 如: v_s ...

  10. python解决最小二乘法的相关问题笔记。

    计算最小二乘法求目标函数的系数,部分代码来源于张若愚老师的<Python科学计算> 题目 某检测装备输入数据: x:0.9, 2.5, 3.3, 4.5, 5.7, 6.9 g:1.1, ...