PostgreSQL use value from previous row if missing

前端 未结 2 516
醉梦人生
醉梦人生 2020-12-19 17:12

I have a following query:

WITH t as (
  SELECT date_trunc(\'hour\', time_series) as trunc 
  FROM generate_series(\'2013-02-27 22:00\'::timestamp, \'2013-02-         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-19 17:56

    Try:

    WITH t as (
      SELECT time_series as trunc 
        FROM generate_series('2013-02-27 22:00'::timestamp, '2013-02-28 2:00', 
                             '1 hour') as time_series
    )
    SELECT DISTINCT ON(t.trunc) t.trunc, e.id
      FROM t
      JOIN event e
        ON e.created < t.trunc 
     ORDER BY t.trunc, e.created DESC
    

    If it is too slow - tell me. I will give you a faster query.

提交回复
热议问题