一尘不染

Postgres中的有效时间序列查询

sql

我的PG数据库中有一个表,看起来像这样:

id | widget_id | for_date | score |

每个引用的小部件都有很多这些项。每个小部件每天始终为1,但仍有差距。

我想要得到的结果是包含自X以来每个日期的所有小部件。这些日期是通过generate系列引入的:

 SELECT date.date::date
   FROM generate_series('2012-01-01'::timestamp with time zone,'now'::text::date::timestamp with time zone, '1 day') date(date)
 ORDER BY date.date DESC;

如果没有给定widget_id的日期条目,我想使用上一个。假设小部件1337在2012-05-10上没有条目,但是在2012-05-08上,那么我希望结果集在2012-05-10上也显示2012-05-08条目:

Actual data:
widget_id | for_date   | score
1312      | 2012-05-07 | 20
1337      | 2012-05-07 | 12
1337      | 2012-05-08 | 41
1337      | 2012-05-11 | 500

Desired output based on generate series:
widget_id | for_date   | score
1336      | 2012-05-07 | 20
1337      | 2012-05-07 | 12
1336      | 2012-05-08 | 20
1337      | 2012-05-08 | 41
1336      | 2012-05-09 | 20
1337      | 2012-05-09 | 41
1336      | 2012-05-10 | 20
1337      | 2012-05-10 | 41
1336      | 2012-05-11 | 20
1337      | 2012-05-11 | 500

最终,我想将其归结为一个视图,这样我每天就有一致的数据集,可以轻松查询。

编辑: 使样本数据和预期结果集更加清晰


阅读 186

收藏
2021-03-17

共1个答案

一尘不染

SQL小提琴

select
    widget_id,
    for_date,
    case
        when score is not null then score
        else first_value(score) over (partition by widget_id, c order by for_date)
        end score
from (
    select
        a.widget_id,
        a.for_date,
        s.score,
        count(score) over(partition by a.widget_id order by a.for_date) c
    from (
        select widget_id, g.d::date for_date
        from (
            select distinct widget_id
            from score
            ) s
            cross join
            generate_series(
                (select min(for_date) from score),
                (select max(for_date) from score),
                '1 day'
            ) g(d)
        ) a
        left join
        score s on a.widget_id = s.widget_id and a.for_date = s.for_date
) s
order by widget_id, for_date
2021-03-17