跟踪状态更改为特定值的日期

问题描述 投票:0回答:1

我有一张桌子

my_table
,如下所示:

                     id                  |          updated_at           | status 
-----------------------------------------+-------------------------------+--------
 ID1                                     | 2023-12-18 18:37:21.724825+00 | I
 ID1                                     | 2024-01-02 23:08:03.50741+00  | I
 ID1                                     | 2024-01-03 23:07:03.892617+00 | I
 ID1                                     | 2024-01-06 23:09:00.877018+00 | A
 ID1                                     | 2024-01-07 23:08:03.134111+00 | A
 ID1                                     | 2024-01-08 23:06:31.164932+00 | A
 ID1                                     | 2024-03-03 23:39:39.138697+00 | A
 ID1                                     | 2024-03-04 23:55:54.582789+00 | F
 ID1                                     | 2024-03-05 23:37:15.70314+00  | F
 ID1                                     | 2024-03-06 23:47:16.814729+00 | F
 ID1                                     | 2024-03-07 23:42:54.349137+00 | A
 ID1                                     | 2024-03-08 23:35:53.188792+00 | A
 ID1                                     | 2024-03-09 23:30:55.979575+00 | A
 ID1                                     | 2024-04-10 08:30:05.329453+00 | F
 ID2                                     | 2024-03-15 23:52:28.005298+00 | A
 ID2                                     | 2024-03-16 23:53:38.031816+00 | A
 ID2                                     | 2024-03-17 23:57:42.556396+00 | A
 ID2                                     | 2024-03-18 13:33:38.264444+00 | A
 ID2                                     | 2024-03-18 22:48:29.366489+00 | A
 ID2                                     | 2024-03-19 15:00:02.19655+00  | F
 ID2                                     | 2024-03-26 18:37:13.514644+00 | A
 ID2                                     | 2024-03-27 15:19:13.823159+00 | F
 ID2                                     | 2024-03-28 15:31:46.841049+00 | F
 ID2                                     | 2024-04-03 07:43:08.253604+00 | P

我想从中选择状态从任何值更改为值

F
的日期,即我期望以下输出:

ID1   | 2024-03-04 23:55:54.582789+00
ID1   | 2024-04-10 08:30:05.329453+00
ID2   | 2024-03-19 15:00:02.19655+00
ID2   | 2024-03-27 15:19:13.823159+00

到目前为止,我成功地获得了每个

id
第一次发生这样的变化,但我无法获得后续的变化。这是我的查询:

    SELECT
        id, updated_at
    FROM (
        SELECT
            *
            , RANK() OVER (PARTITION BY id ORDER BY updated_at ASC) AS rank_
        FROM ( SELECT * FROM my_table WHERE status='F' ) t_sub
    ) T
    WHERE rank_ = 1

如何更改它不仅可以让每次

id
第一次变成
F
状态,还能让第二次、第三次、第四次等等…都变成
F

postgresql partitioning status ranking-functions
1个回答
0
投票

查找

status
值与
lag(status)over(partition by id order by updated_at)
窗口函数不同的行:demo

select id,updated_at 
from(select *
           ,status<>lag(status,1,status)over w1 and status='F' as changed_to_f
     from my_table
     window w1 as (partition by id order by updated_at) ) as subquery
where changed_to_f
order by 1,2;
id 更新于
ID1 2024-03-04 23:55:54.582789+00
ID1 2024-04-10 09:30:05.329453+01
ID2 2024-03-19 15:00:02.19655+00
ID2 2024-03-27 15:19:13.823159+00
© www.soinside.com 2019 - 2024. All rights reserved.