如何基于蜂巢中的3列查找先前的日期值

时间:2018-10-15 09:25:56

标签: hadoop hive hiveql

我想基于3列在我的目标表中找到上一个日期值。随附的屏幕截图中说明了示例和场景。

enter image description here

请帮助。

2 个答案:

答案 0 :(得分:0)

这可以通过lag完成。

select t.*,lag(date) over(partition by id,function_id,key order by date) as prev_date
from tbl t

答案 1 :(得分:0)

检查以下内容是否适合您。

> create table vimarsh (id int, function_id int, key string, dt date);

> insert into vimarsh 
select 123,342,'test1','2018-10-15'
union all 
select 1234,35434,'test2','2018-10-16'
union all
select 2131,8907,'test3','2018-10-17'
union all
select 123,342,'test1','2018-10-18';

> select * from vimarsh;

+-------------+----------------------+--------------+-------------+--+
| vimarsh.id  | vimarsh.function_id  | vimarsh.key  | vimarsh.dt  |
+-------------+----------------------+--------------+-------------+--+
| 123         | 342                  | test1        | 2018-10-15  |
| 1234        | 35434                | test2        | 2018-10-16  |
| 2131        | 8907                 | test3        | 2018-10-17  |
| 123         | 342                  | test1        | 2018-10-18  |
+-------------+----------------------+--------------+-------------+--+

>  select id, function_id,key, dt, lag(dt) over(partition by id,function_id,key order by dt) as prev_date from vimarsh;
INFO  : OK
+-------+--------------+--------+-------------+-------------+--+
|  id   | function_id  |  key   |     dt      |  prev_date  |
+-------+--------------+--------+-------------+-------------+--+
| 123   | 342          | test1  | 2018-10-15  | NULL        |
| 123   | 342          | test1  | 2018-10-18  | 2018-10-15  |
| 1234  | 35434        | test2  | 2018-10-16  | NULL        |
| 2131  | 8907         | test3  | 2018-10-17  | NULL        |
+-------+--------------+--------+-------------+-------------+--+
4 rows selected (35.585 seconds)
相关问题