0
我有一个样本数据如下,并希望得到所需的o/p,请帮我一些想法。滞后函数得到最后一个不同的值(红移)
我想第三的prev_diff_value的O/P,第4行是2015年1月1日00:00:00,而不是2015年1月2日00:00:00。
with dat as (
select 1 as id,'20150101 02:02:50'::timestamp as dt union all
select 1,'20150101 03:02:50'::timestamp union all
select 1,'20150101 04:02:50'::timestamp union all
select 1,'20150102 02:02:50'::timestamp union all
select 1,'20150102 02:02:50'::timestamp union all
select 1,'20150102 02:02:51'::timestamp union all
select 1,'20150103 02:02:50'::timestamp union all
select 2,'20150101 02:02:50'::timestamp union all
select 2,'20150101 03:02:50'::timestamp union all
select 2,'20150101 04:02:50'::timestamp union all
select 2,'20150102 02:02:50'::timestamp union all
select 1,'20150104 02:02:50'::timestamp
)-- select * from dat
select id , dt , lag(trunc(dt)) over(partition by id order by dt asc) prev_diff_value
from dat
order by id,dt desc
O/P :
id dt prev_diff_value
1 2015-01-04 02:02:50 2015-01-03 00:00:00
1 2015-01-03 02:02:50 2015-01-02 00:00:00
1 2015-01-02 02:02:51 2015-01-02 00:00:00
1 2015-01-02 02:02:50 2015-01-02 00:00:00
1 2015-01-02 02:02:50 2015-01-01 00:00:00
嗨,你能更好地解释你想在prev_diff_value列中看到什么吗?滞后函数将前一个作为参考,因此它工作正常,您要求该红移。你为什么想回到一步?也许你可以按年 - 月 - 日分组来划分,所以2015-01-02 02:02:50和2015-01-02 02:02:51会被认为是一样的吗? – MiloBellano
好吧..我知道它返回正确的结果,但prev-diff-value中我期望的值是前一天或早些日子而不是前一行 – Fact