【问题标题】:Generate first and last status in separate columns SQL在单独的列 SQL 中生成第一个和最后一个状态
【发布时间】:2020-11-14 14:37:46
【问题描述】:

我有以下数据:

使用下面的查询,我得到以下结果:

我想得到什么结果:

如何修改我的查询以获取最后一个表?

with minmax as (select t1.*,
                       case when rank=(min(rank) over (partition by id)) then status end   as first_status,
                       case when rank=(min(rank) over (partition by id)) then status_date end   as first_status_date,
                       case when rank=(max(rank) over (partition by id)) then status end   as last_status,
                       case when rank=(max(rank) over (partition by id)) then status_date end   as last_status_date,
                       max(rank) over (partition by id) as max
                from (select id,
                             status_date,
                             status,
                             rank()
                             over (partition by id order by id, status_date) as rank
                      from history_table as hist
                     ) as t1
                group by 1, 2, 3, 4
                order by id, status_date)
select distinct 
                id,
                first_status,
                first_status_date,
                last_status,
                last_status_date
from id_table as idt
left join minmax as mm on idt.id=mm.id

【问题讨论】:

    标签: sql postgresql date pivot window-functions


    【解决方案1】:

    您应该可以使用如下 first_value 和 last_value 分析函数来使用它

    select distinct 
           id
          ,first_value(status) over(partition by id order by status_date) as first_status
          ,first_value(status_date) over(partition by id order by status_date) as first_status_date
          ,last_value(status) over(partition by id order by status_date      range between unbounded preceding and unbounded following) as last_status
          ,last_value(status_date) over(partition by id order by status_date range between unbounded preceding and unbounded following) as last_status_date
      from t   
    order by id
    
    +-----+--------------+---------------------+-------------+---------------------+
    | id  | first_status |  first_status_date  | last_status |  last_status_date   |
    +-----+--------------+---------------------+-------------+---------------------+
    | 123 | appointment  | 2018-01-09 13:18:09 | left        | 2018-09-20 14:51:13 |
    | 547 | appointment  | 2018-02-14 09:43:15 | sold        | 2018-06-06 07:36:56 |
    +-----+--------------+---------------------+-------------+---------------------+
    

    这是一个 db fiddle 链接

    https://dbfiddle.uk/?rdbms=postgres_12&fiddle=564f022fcf2d84afb054b0a48fa438ca

    【讨论】:

    • 为什么为 last_value 添加了range between unbounded preceding and unbounded following 而不是 first_value?
    • 它是升序排序,所以除非指定窗口范围,否则不会包含最后一个值
    【解决方案2】:

    我认为最好使用窗口函数和distinct:

    select distinct
        id,
        first_value(status) over(partition by id order by status_date) first_status,
        min(status_date)         over(partition by id) first_status_date,
        first_value(status) over(partition by id order by status_date desc) last_status,
        max(status_date)         over(partition by id) last_status_date
    from history_table
    order by id
    

    更经典的方法是 row_number() 和条件聚合 - 但我怀疑这里的效率有点低(而且它也更长):

    select
        id,
        min(status) filter(where rn_asc = 1) first_status,
        min(status_date) first_status_date,
        min(status) filter(where rn_desc = 1) last_status,
        min(status_date) last_status_date,
    from (
        select 
            h.*,
            row_number() over(partition by id order by status_date) rn_asc,
            row_number() over(partition by id order by status_date desc) rn_desc
        from history_table h
    ) h
    where 1 in (rn_asc, rn_desc)
    group by id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2021-03-18
      相关资源
      最近更新 更多