【问题标题】:PostgreSQL - time series interpolationPostgreSQL - 时间序列插值
【发布时间】:2016-07-15 07:45:12
【问题描述】:

我有一个表格“performances”,其中包含“date”和“count”列。 但是,行是稀疏的,即有很多天没有行,这隐含地意味着计数 = 0。 在此运行时有没有可以执行的查询:

date       count
2016-7-15  3
2016-7-12  1
2016-7-11  2

会给我这个:

date       count
2016-7-15  3
2016-7-14  0
2016-7-13  0
2016-7-12  1
2016-7-11  2

?

【问题讨论】:

标签: postgresql


【解决方案1】:

您可以使用generate_series()left join

with q as (<your query here>)
select s.dte, coalesce(q.count, 0) as count
from (select generate_series(min(q.date), max(q.date), interval '1 day') as dte
      from q
     ) s left join
     q
     on s.dte = q.date;

【讨论】:

    猜你喜欢
    • 2012-10-15
    • 2017-08-13
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2018-05-14
    相关资源
    最近更新 更多