-- 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. mongodb 降序

    降序排序 db.mycol.find({},{,_id:}).sort({})// 1用于升序排列,而-1用于降序. 示例代码: var user = await Kitten.find({ type ...

  2. js 控制标记样式

    做一个变色的标签 鼠标移入变为灰色,移除变回原来的颜色,点击变成黑色,再点击变回,如果变成黑色不受移入移除影响. <body> <div class="bt1" ...

  3. IFC4 在线参考手册

    国内地址1:http://www.vfkjsd.cn/ifc/ifc4/index.htm 国内地址2:http://www.bim-times.com/ifc/ifc4/index.htm 官方地址 ...

  4. p2501 [HAOI2006]数字序列

    传送门 分析 https://www.luogu.org/blog/FlierKing/solution-p2501 对于第二问的感性理解就是有上下两条线,一些点在上面的线的上面或者下面的线的下面,然 ...

  5. EZOJ #80

    传送门 分析 经典的树型DP 我们记录dp[i][0/1]表示i的子树中到i的长度分别为偶数和奇数的长度和 dp2[i][0/1]则表示不在i的子树中的点到i的长度分别为偶数和奇数的长度和 然后根据边 ...

  6. Bulma 中的媒体查询

    在 Bulma 中将设备分为 6 类:手机.平板.触屏设备.桌面.宽屏和大屏.提供了四个阈值. // sass/utilities/variables.sass $tablet: 769px !def ...

  7. Ubuntu学习小结(二)PostgreSQL的使用,进程的查看关闭,编辑器之神Vim入门

    距离上次发布文章已经过去了很久.在过去的半年中,虽然写的代码不多,但是在接触了计算机一些其他的知识,包括数据库.网络之后,感觉能够融会贯通,写代码水平又有了一定的提高.接下来,将会发表几篇文章,简单介 ...

  8. asp.net core tags 扩展之 id 和 name

    asp.net core 页面 TagHelper  的 Id 和 Name 属性扩展 . [HtmlTargetElement(Attributes = "asp-name")] ...

  9. android studio 程序员有福了—从layout自动生成viewholder类

    狂点这里下载 超级牛逼的插件啊,比那些使用SparseArray的强太多了! 在android studio 1.0上测试,没有问题. 不说了直接说功能 Android Toolbox Plugin ...

  10. 解决“System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本”的问题

    以server2008为例: 首先确保使用sqlplus能访问数据库. 1.管理工具->计算机管理->本地用户和组->组->administrators属性,添加,高级,立即查 ...