To understand a business process with several statuses,
I want to get the rows with the following rules based on created_at
column:
First row of status
‘created’
Last row of
‘missing_info’
after‘created’
(row_no 4)First row of
‘pending’
(row_no 5)Last row of
‘missing_info’
after‘pending’
(row_no 7)First row of
‘pending’
after'missing_info'
(row_no 8)Last row of
‘successful’
(row_no 10)
Below I highlighted the rows I want to retrieve.
Here is the example data on DB-FIDDLE
Here is the general flow: created > missing_info > pending > successful. But can be only like this too: created > successful.
I know that I can use QUALIFY
with window functions and can get 'created'
and 'successful'
as below. But I don’t know how to get interim statuses. How can I achieve the desired output?
created AS(
SELECT *
FROM t1
WHERE status = 'created'
QUALIFY ROW_NUMBER() OVER (PARTITION BY STATUS, id ORDER BY created_at) = 1 )
Please note that created
and successful
are start and end statuses, so there will be only one row in the output. Others like missing_info
or pending
are interim statuses, so can be multiple of them in the desired output.
EDIT:
To understand a business process with several statuses,
I want to get some of the rows with the following two rules based on created_at
column:
last row of status ‘missing_info’ before ‘pending’ (row 2)
pending (row 3)
last row of status ‘missing_info’ before ‘pending’ (row 5)
pending (row 6)
Example data:
WITH t1 AS (
SELECT 'A' AS id, 'missing_info' AS status, '2021-07-15 11:10:00'::timestamp AS created_at UNION ALL
SELECT 'A' AS id, 'missing_info' AS status, '2021-07-15 11:20:00'::timestamp AS created_at UNION ALL
SELECT 'A' AS id, 'pending' AS status, '2021-07-15 11:30:00'::timestamp AS created_at UNION ALL
SELECT 'A' AS id, 'missing_info' AS status, '2021-07-15 12:10:00'::timestamp AS created_at UNION ALL
SELECT 'A' AS id, 'missing_info' AS status, '2021-07-15 12:20:00'::timestamp AS created_at UNION ALL
SELECT 'A' AS id, 'pending' AS status, '2021-07-15 12:30:00'::timestamp AS created_at
)
SELECT *
FROM t1
Desired output: