LAG function and GROUP BY

前端 未结 1 1262
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-14 08:19

I have a table like this,

 event_id |          date          
----------+------------------------
  1703702 | 2013-06-25 07:50:57-04
  3197588 | 2013-06-25          


        
1条回答
  •  有刺的猬
    2020-12-14 08:34

    WITH diffs as (
        SELECT
            event_id,
            date - lag(date) over (partition BY event_id ORDER BY date) as difference
        FROM
            TABLE
    )
    SELECT
        event_id,
        array_agg( difference ) as all_diffs
    FROM
        diffs
    GROUP BY event_id;
    

    Should work.

    0 讨论(0)
提交回复
热议问题