如何使用 first_value 和重置
how to use first_value with reset
WITH t AS
(
SELECT
*
FROM
(VALUES ('2021-10-09 23:58:34.000', '2021-10-10 00:00:00.000', 'task_completed', '00001'),
('2021-10-10 00:00:01.000', '2021-10-10 00:00:05.000', 'task_completed', '00001'),
('2021-10-10 00:00:06.000', '2021-10-10 00:00:25.000', 'task_completed', '00001'),
('2021-10-10 00:00:26.000', '2021-10-10 00:00:45.000', 'task_not_completed', '00001'),
('2021-10-10 00:00:46.000', '2021-10-10 00:01:00.000', 'task_not_completed', '00001'),
('2021-10-10 00:01:01.000', '2021-10-10 00:01:10.000', 'task_completed', '00001'),
('2021-10-10 00:01:11.000', '2021-10-10 00:01:15.000', 'task_completed', '00001')) AS t(start_time, end_time, task_state, person_id)
ORDER BY
1
)
SELECT *
FROM t
我正在尝试获得如下所示的聚合结果:
start_time
end_time
task_state
'2021-10-09 23:58:34.000
2021-10-10 00:00:25.000'
task_completed
'2021-10-10 00:00:26.000
2021-10-10 00:01:00.000'
task_not_completed
'2021-10-10 00:01:01.000
2021-10-10 00:01:15.000'
task_completed
我曾尝试使用 first_value 函数,但它似乎只是为所有 task_state.[=14 提供值 '2021-10-09 23:58:34.000' =]
不确定我做错了什么。这是我尝试过的:
SELECT
start_time,
FIRST_VALUE(start_time) OVER (ORDER BY start_time) AS end_time,
MIN(task_state) OVER (ORDER BY start_time) AS state
FROM
t
为此寻求帮助
这是一个 gaps & island
问题:
SELECT person_id, task_state, MIN(start_time) start_time, MAX(end_time) end_time
from (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.person_id ORDER BY start_time)
- ROW_NUMBER() OVER (PARTITION BY t.person_id, t.task_state ORDER BY start_time) AS taskgroups
FROM t
) tt GROUP BY taskgroups, person_id, task_state
db<>fiddle here
WITH t AS
(
SELECT
*
FROM
(VALUES ('2021-10-09 23:58:34.000', '2021-10-10 00:00:00.000', 'task_completed', '00001'),
('2021-10-10 00:00:01.000', '2021-10-10 00:00:05.000', 'task_completed', '00001'),
('2021-10-10 00:00:06.000', '2021-10-10 00:00:25.000', 'task_completed', '00001'),
('2021-10-10 00:00:26.000', '2021-10-10 00:00:45.000', 'task_not_completed', '00001'),
('2021-10-10 00:00:46.000', '2021-10-10 00:01:00.000', 'task_not_completed', '00001'),
('2021-10-10 00:01:01.000', '2021-10-10 00:01:10.000', 'task_completed', '00001'),
('2021-10-10 00:01:11.000', '2021-10-10 00:01:15.000', 'task_completed', '00001')) AS t(start_time, end_time, task_state, person_id)
ORDER BY
1
)
SELECT *
FROM t
我正在尝试获得如下所示的聚合结果:
start_time | end_time | task_state |
---|---|---|
'2021-10-09 23:58:34.000 | 2021-10-10 00:00:25.000' | task_completed |
'2021-10-10 00:00:26.000 | 2021-10-10 00:01:00.000' | task_not_completed |
'2021-10-10 00:01:01.000 | 2021-10-10 00:01:15.000' | task_completed |
我曾尝试使用 first_value 函数,但它似乎只是为所有 task_state.[=14 提供值 '2021-10-09 23:58:34.000' =]
不确定我做错了什么。这是我尝试过的:
SELECT
start_time,
FIRST_VALUE(start_time) OVER (ORDER BY start_time) AS end_time,
MIN(task_state) OVER (ORDER BY start_time) AS state
FROM
t
为此寻求帮助
这是一个 gaps & island
问题:
SELECT person_id, task_state, MIN(start_time) start_time, MAX(end_time) end_time
from (
SELECT *
, ROW_NUMBER() OVER (PARTITION BY t.person_id ORDER BY start_time)
- ROW_NUMBER() OVER (PARTITION BY t.person_id, t.task_state ORDER BY start_time) AS taskgroups
FROM t
) tt GROUP BY taskgroups, person_id, task_state
db<>fiddle here