更新时间:2023-02-05 09:33:08
注意:
D,E
,我不明白,当上一行没有相同标签时,应该如何返回是 D, E
, I do not understand, how this should return 'yes' when previous row does not have same label数据:
drop table if exists tmp.test;
create table tmp.test (id int, ddate date, label varchar, ttime time);
insert into tmp.test values
(1, '2014/6/4','A','12:05:56'),
(2, '2014/6/4','A','23:02:32'),
(3, '2014/6/4','B','8:39:25'),
(4, '2014/6/4','B','12:36:37'),
(5, '2014/6/4','C','12:20:43'),
(6, '2014/6/4','C','12:56:44'),
(7, '2014/6/4','D','20:52:22'),
(8, '2014/6/4','E','22:25:30'),
(9, '2014/6/4','F','12:16:15'),
(10, '2014/6/4','F','12:31:09'),
(11, '2014/6/4','F','7:12:06'),
(12, '2014/6/4','G','7:48:32'),
(13, '2014/6/4','H','17:58:11');
查询:
select
id,
ddate,
label,
ttime,
case when (lag(ttime) over(partition by label order by id))::interval
+ ttime::interval > interval '24 hours' then 'yes' else 'no' end
-- ,(lag(ttime) over(partition by label order by ttime))::interval + ttime::interval
from
tmp.test
说明:
lag
函数将在给定分区的上一行中获取值。在我们的例子中,分区由标签定义。 ::
将更改时间
输入 interval
,这样我们可以增加时间并获得超过24小时的时间。是
或否
。lag
function will get value in previous row for given partition. In our case, partition is defined by label.::
will change time
type into interval
, so we can add time and get more than 24 hours.yes
or no
.更新:
select
id,
ddate,
label,
ttime,
case when lead(label) over(partition by label order by id) is null then 'no' else 'yes' end
from
tmp.test