答案 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)