2

I'm wondering how to do a group by query where I need to group all the data of the same minute in my db to sum all of them.

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}

Doing this query

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

I want to get 5 in sum, which is the total in the minute 44 understand?

1 Answer 1

1

I think you want 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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.