微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有一张 readings 表。定义为:

   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;

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

电流输出

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

 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 是一个 dbfiddle。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?