【发布时间】:2021-05-01 21:13:44
【问题描述】:
我有一个表,其中包含 3 种状态的设备,通过、失败和警告。
| Device | Status | Date |
|---|---|---|
| Device1 | Pass | 12/1/2020 |
| Device2 | Fail | 12/1/2020 |
| Device3 | Warning | 12/1/2020 |
| Device1 | Fail | 12/2/2020 |
| Device2 | Warning | 12/2/2020 |
| Device3 | Pass | 12/2/2020 |
我想根据日常状态生成设备数量的趋势图。每天在所有设备上进行计数。上表将重复多个日期的设备数据。
示例:
我想生成一个堆叠条形图,它将显示通过、失败或警告的设备数量。需要获取一个查询,我可以使用它来获取回复,包括DateTime、失败设备的数量、通过的设备数量、在一定日期范围内发出警告的设备数量。
select * (select count(*) from status_table where overall_status = 'Fail' and startDate > "" and endDate < "") as failedCount,
(select count(*) from status_table where overall_status = 'Warning' and startDate > "" and endDate < "") as WarningCount,
(select count(*) from status_table where overall_status = 'Pass' startDate > "" and endDate < "") as passCount from status_table
有没有更好的解决方案?
【问题讨论】:
-
设备数是指每个设备的行数,对吧?请始终声明您的 Postgres 版本。 每日状态您的意思是.. 表中的每个设备每天一行?或者究竟是什么?
标签: sql postgresql aggregate aggregate-filter