2017-12-17 24 views
2

나는 그 (것)들을 합계하기 위하여 나의 db에있는 동일한 분의 모든 자료를 분류 할 필요가있는 질문에 의하여 그룹을하는 방법 생각해보고있다.시간 소인 별 포스트 그레스에서 쿼리

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)는 이해 분 총입니다

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 

내가 합계 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

감사합니다, 그것은 일 –