【问题标题】:Is there a way to retrieve the latest of the first `status` value from the table?有没有办法从表中检索最新的第一个“状态”值?
【发布时间】:2022-01-16 02:51:48
【问题描述】:

我一直在尝试编写一个查询,该查询返回第一条状态匹配记录的latest 与查询本身。假设我有下面的表记录,我想检索APP_IDINSERTED_AT 列,其中STATUS 应该是Offer Sent 中的第一个,发生在最新的Selectable 状态之后,如果有任何Offer Declined 都应该忽略它。

ID APP_ID INSERTED_AT UPDATED_AT STATUS
1 209377 2021-05-13 20:57:30.000 +0530 2021-05-13 20:57:30.000 +0530 Selectable
2 209377 2021-05-13 20:57:58.000 +0530 2021-05-13 20:57:58.000 +0530 Selectable
3 209377 2021-05-14 18:40:08.000 +0530 2021-05-14 18:40:08.000 +0530 Offer Eligible
4 209377 2021-05-14 18:40:14.000 +0530 2021-05-14 18:40:14.000 +0530 Offer Sent
5 209377 2021-05-15 18:57:50.000 +0530 2021-05-15 18:57:50.000 +0530 Offer Sent
6 209377 2021-05-15 20:44:29.000 +0530 2021-05-15 20:44:29.000 +0530 Offer Sent
7 209377 2021-05-17 22:45:13.000 +0530 2021-05-17 22:45:13.000 +0530 Offer Accepted
8 110011 2021-05-13 20:57:30.000 +0530 2021-05-13 20:57:30.000 +0530 Selectable
9 110011 2021-05-13 20:57:58.000 +0530 2021-05-13 20:57:58.000 +0530 Offer Eligible
10 110011 2021-06-14 18:40:08.000 +0530 2021-05-14 18:40:08.000 +0530 Offer Sent
11 300110 2021-05-14 18:40:14.000 +0530 2021-05-14 18:40:14.000 +0530 Selectable
12 300110 2021-05-15 18:57:50.000 +0530 2021-05-15 18:57:50.000 +0530 Offer Eligible
13 300110 2021-05-15 20:44:29.000 +0530 2021-05-15 20:44:29.000 +0530 Offer Sent
14 300110 2021-05-17 22:45:13.000 +0530 2021-05-17 22:45:13.000 +0530 Offer Declined

这是我正在寻找的预期结果

APP_ID INSERTED_AT STATUS
209377 2021-05-14 18:40:14.000 +0530 Offer Sent
110011 2021-05-14 18:40:08.000 +0530 Offer Sent
300110 nil Offer Declined

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    试试这个

        Select *, max(case when status ='Selected' then
         When lag(status) over (order by 
         null) ='Offer Sent' then 'Offer Sent' 
         Else nil end) 
       From
       (
       Select APP_ID, INSERTED_AT,  
       Status 
        From table
        Where status in ('Selected', 'Offer Sent', 'Offer 
         Declined')) 
         
    

    【讨论】:

      猜你喜欢
      • 2015-03-27
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 2016-02-02
      • 1970-01-01
      • 2021-11-16
      • 2021-04-16
      相关资源
      最近更新 更多