gpt4 book ai didi

sql - 按时间戳分钟在 postgres 中查询

转载 作者:行者123 更新时间:2023-11-29 12:17:03 24 4
gpt4 key购买 nike

我想知道如何通过查询进行分组,我需要在我的数据库中对同一分钟的所有数据进行分组以对所有数据求和。

ws_controller_hist=>  SELECT timestamp, type, medium from default_dataset where type like 'status_devices' and timestamp> current_timestamp - interval '60 minutes' and organization_id = '9fc02db4-c3df-4890-93ac-8dd575ca5638' order by timestamp asc;

timestamp | type | medium
------------------------+----------------+--------
2017-12-17 12:44:00+00 | status_devices | {1,0}
2017-12-17 12:44:00+00 | status_devices | {1,0}
2017-12-17 12:44:00+00 | status_devices | {1,0}
2017-12-17 12:44:01+00 | status_devices | {1,0}
2017-12-17 12:44:01+00 | status_devices | {1,0}
2017-12-17 12:44:10+00 | status_devices | {0,1}
2017-12-17 12:44:10+00 | status_devices | {0,1}

做这个查询

ws_controller_hist=>  SELECT timestamp, type, sum(medium[1]) from default_dataset where type like 'status_devices' and timestamp> current_timestamp - interval '60 minutes' and organization_id = '9fc02db4-c3df-4890-93ac-8dd575ca5638' group by timestamp, type order by timestamp asc;

timestamp | type | sum
------------------------+----------------+-----
2017-12-17 12:44:00+00 | status_devices | 3
2017-12-17 12:44:01+00 | status_devices | 2
2017-12-17 12:44:10+00 | status_devices | 0

我要求和,44分钟内的总和是多少,明白吗?

最佳答案

我想你想要date_trunc():

select date_trunc('minute', timestamp) as timestamp_min, type, 
sum(medium[1])
from default_dataset
where type like 'status_devices' and
timestamp > current_timestamp - interval '60 minutes' and
organization_id = '9fc02db4-c3df-4890-93ac-8dd575ca5638'
group by timestamp_min, type
order by timestamp_min asc;

关于sql - 按时间戳分钟在 postgres 中查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47855429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com