2017-12-17 6 views
2

私はすべてのデータを集計するためにデータベース内の同じ分のすべてのデータをグループ化する必要があるクエリでグループを行う方法を知っています。このクエリタイムスタンプ分のポストグアでのクエリ

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 

を行う

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} 

私は44が理解分で合計である、合計で5を取得したいですか?

答えて

1

は、私はあなたが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; 
+0

おかげで、それが働きました –

関連する問題