【发布时间】:2021-10-12 13:00:58
【问题描述】:
我有一张桌子messages,看起来像:
| status | created_at | updated_at |
|---|---|---|
| sent | timestamp | timestamp |
| queued | timestamp | timestamp |
| failed | timestamp | timestamp |
我想要一个以易于绘制图表的方式输出值的查询,我想出了这个查询:
SELECT date_trunc('hour', created_at) as hour_created, count(created_at), status
FROM messages
WHERE created_at >= current_timestamp - interval '1 day'
GROUP BY (status, hour_created)
该查询输出以下内容:
| hour_created | count | status |
|---|---|---|
| 2021-10-11 08:00:00-04 | 10 | sent |
| 2021-10-11 09:00:00-04 | 95 | queued |
| 2021-10-11 10:00:00-04 | 174 | sent |
| 2021-10-11 12:00:00-04 | 1 | sent |
| 2021-10-11 13:00:00-04 | 1 | queued |
| 2021-10-11 13:00:00-04 | 2 | sent |
| 2021-10-11 14:00:00-04 | 1 | sent |
| 2021-10-11 18:00:00-04 | 2 | sent |
注意小时数的差距,我想用所有可能的状态(排队、失败和发送)和零来填充那些时间
【问题讨论】:
标签: sql postgresql datetime