如何根据配置单元中的3列查找上一个日期值

问题描述 投票:0回答:2

我想基于3列找出目标表中的上一个日期值。附带的屏幕截图中解释了示例和场景。

enter image description here

请帮忙。

hadoop hive hiveql
2个回答
0
投票

这可以用lag完成。

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.