即使不存在任何值,也为日期范围内的每一天获取一行

Get a row for each day of date range even when no values exist

我有一个readingstable。它被定义为:

   Column   |            Type             | Collation | Nullable | Default
------------+-----------------------------+-----------+----------+---------
 created_at | timestamp without time zone |           | not null |
 device     | character varying(25)       |           | not null |
 type       | character varying(25)       |           | not null |
 value      | numeric                     |           | not null |

它有这样的数据:

     created_at      |  device   |    type     |    value
---------------------+-----------+-------------+-------------
 2021-03-16 07:46:47 | 465125783 | temperature |        36.5
 2021-03-16 07:51:48 | 465125783 | temperature | 36.40000153
 2021-03-16 07:52:47 | 465125783 | temperature | 36.40000153
 2021-03-16 07:53:47 | 465125783 | temperature | 36.29999924
 2021-03-24 17:53:47 | 123456789 | pressure    |          79
 2021-03-24 17:54:48 | 123456789 | pressure    |          77
 2021-03-28 05:38:48 | 123456789 | flow        |          12
 2021-03-28 05:45:48 | 123456789 | flow        |          14
 2021-03-28 05:49:47 | 123456789 | pressure    |          65
 2021-03-28 05:50:47 | 123456789 | flow        |          32
 2021-03-28 05:51:47 | 123456789 | flow        |          40

当前查询

到目前为止,我有以下查询:

select created_at::date, device,
       avg(value) filter (where type = 'temperature') as temperature,
       avg(value) filter (where type = 'pressure') as pressure,
       avg(value) filter (where type = 'flow') as flow
from readings
where device = '123456789' and created_at::date > created_at::date - interval '14 days'
group by created_at::date, device
order by created_at::date desc;

查询计算出过去两周每个 type 的每日平均值 value

当前输出

当我 运行 查询时,我得到以下信息:

 created_at |  device   | temperature |      pressure       |        flow
------------+-----------+-------------+---------------------+---------------------
 2021-03-28 | 123456789 |             | 65.0000000000000000 | 24.5000000000000000
 2021-03-24 | 123456789 |             | 78.0000000000000000 |

期望输出

我真正想要的是过去两周每个日期的 ,所以我想结束:

 created_at |  device   | temperature |      pressure       |        flow
------------+-----------+-------------+---------------------+---------------------
 2021-04-02 | 123456789 |             |                     | 
 2021-04-01 | 123456789 |             |                     | 
 2021-03-31 | 123456789 |             |                     | 
 2021-03-30 | 123456789 |             |                     | 
 2021-03-29 | 123456789 |             |                     | 
 2021-03-28 | 123456789 |             | 65.0000000000000000 | 24.5000000000000000
 2021-03-27 | 123456789 |             |                     | 
 2021-03-26 | 123456789 |             |                     | 
 2021-03-25 | 123456789 |             |                     | 
 2021-03-24 | 123456789 |             | 78.0000000000000000 | 
 2021-03-23 | 123456789 |             |                     | 
 2021-03-22 | 123456789 |             |                     | 
 2021-03-21 | 123456789 |             |                     | 
 2021-03-20 | 123456789 |             |                     | 

我怎样才能做到这一点?

我有一个db-fiddle

使用generate_series():

select gs.dte, '123456789' as device,
       avg(value) filter (where type = 'temperature') as temperature,
       avg(value) filter (where type = 'pressure') as pressure,
       avg(value) filter (where type = 'flow') as flow
from generate_series('2021-03-20'::date, '2021-04-02'::date, interval '1 day') gs(dte) left join
     readings r
     on r.device = '123456789' and
        r.created_at::date = gs.dte
group by gs.dte
order by gs.dte desc;

Here 是一个 db<>fiddle.