使用 cte 和子查询查找特定条件下的预期输出

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

我有一个名为“origin_polis”的表,这是创建该表并插入示例值的查询

CREATE TABLE origin_polis (
    cif INT,
    no_polis TEXT,
    status TEXT,
    tgl_produksi DATE,
    tgl_awal DATE,
    tgl_akhir DATE
);

INSERT INTO origin_polis (cif, no_polis, status, tgl_produksi, tgl_awal, tgl_akhir)
VALUES
    (100, 'aaa', 'new', '2020-01-13', '2019-02-19', '2022-05-16'),
    (100, 'aaa', 'extend', '2020-01-13', '2020-01-19', '2023-03-13'),
    (100, 'aab', 'new', '2020-01-15', '2020-01-20', '2023-03-15'),
    (101, 'aba', 'new', '2019-05-17', '2019-01-25', '2023-01-15'),
    (101, 'abb', 'new', '2021-06-18', '2020-06-24', '2023-01-10'),
    (102, 'abc', 'new', '2021-01-15', '2021-01-13', '2022-01-13'),
    (102, 'abc', 'extend', '2022-01-17', '2022-01-15', '2023-01-13'),
    (102, 'abc', 'cancel', '2022-01-17', '2022-01-15', '2023-01-13'),
    (103, 'abd', 'new', '2022-04-19', '2022-03-15', '2023-01-14'),
    (103, 'abd', 'cancel', '2022-04-19', '2022-03-15', '2023-01-14'),
    (103, 'abe', 'new', '2022-06-25', '2022-07-10', '2023-01-29'),
    (104, 'aca', 'new', '2022-01-02', '2022-01-01', '2023-09-04'),
    (104, 'aca', 'change', '2022-01-04', '2022-01-01', '2023-09-04'),
    (104, 'aca', 'cancel', '2022-01-06', '2022-01-01', '2023-09-04'),
    (104, 'aca', 'renew', '2022-01-10', '2022-01-01', '2023-10-04'),
    (104, 'acb', 'new', '2022-01-04', '2022-01-02', '2023-10-05'),
    (105, 'ada', 'new', '2022-02-02', '2022-02-01', '2023-08-04'),
    (105, 'ada', 'change', '2022-02-04', '2022-02-01', '2023-08-04'),
    (105, 'ada', 'cancel', '2022-02-06', '2022-02-01', '2023-08-04'),
    (105, 'ada', 'renew', '2022-02-10', '2022-02-01', '2023-09-08'),
    (105, 'ada', 'cancel', '2022-02-11', '2022-02-01', '2023-09-08'),
    (105, 'ada', 'renew', '2022-02-13', '2022-02-10', '2023-10-09'),
    (105, 'ada', 'change', '2022-02-15', '2022-02-10', '2023-11-10');

我想在 postgresql 中进行查询,所以输出是这样的

到岸价格 无城邦 状态 tgl_产品 tgl_awal tgl_akhir
104 阿卡 2022-01-02 2022-01-01 2023-09-04
104 阿卡 改变 2022-01-04 2022-01-01 2023-09-04
104 阿卡 取消 2022-01-06 2022-01-01 2023-09-04
104 阿卡 续订 2022-01-10 2022-01-01 2023-10-04
105 阿达 改变 2022-02-04 2022-02-01 2023-08-04
105 阿达 取消 2022-02-06 2022-02-01 2023-08-04
105 阿达 续订 2022-02-10 2022-02-01 2023-09-08
105 阿达 取消 2022-02-11 2022-02-01 2023-09-08
105 阿达 续订 2022-02-13 2022-02-10 2023-10-09
105 阿达 改变 2022-02-15 2022-02-10 2023-11-10

这是规则,如何获得预期的输出

  1. 选择其中存在 status = 'cancel' 的 no_polis 列
  2. 之后,过滤最后一个 tgl_produksi 不是 status = 'cancel' 的地方

好吧,我知道如何通过此查询获取第一个规则

select *
from origin_polis t1
where exists (select *
            from origin_polis t2
            where t1.no_polis = t2.no_polis
            and status = 'cancel');

但是,我不知道如何将第二条规则包含在查询中, 我尝试过使用 chatgpt,但它的输出不是我所期望的

sql postgresql subquery common-table-expression
1个回答
0
投票

如果问题中所需的输出包含错误,则可以通过 CTE 中的窗口函数应用这些规则,该窗口函数通过

no_polis
将后续各行的状态与当前行链接起来。然后可以使用显示的
where
子句选择要保留的记录:

with links as (
  select *,
         lead(status) over (partition by no_polis
                                order by tgl_produksi) as next_status
    from origin_polis
)
select cif, no_polis, status, tgl_produksi, tgl_awal, tgl_akhir
  from links
 where    (status = 'cancel')
       or (next_status = 'cancel');

工作小提琴

© www.soinside.com 2019 - 2024. All rights reserved.