Re: Group by range in hour of day
От | Paul Jungwirth |
---|---|
Тема | Re: Group by range in hour of day |
Дата | |
Msg-id | 550864A0.3050908@illuminatedcomputing.com обсуждение исходный текст |
Ответ на | Re: Group by range in hour of day (Israel Brewster <israel@ravnalaska.net>) |
Ответы |
Re: Group by range in hour of day
|
Список | pgsql-general |
So next question: how do I get the "active" time per hour from this? I think you just SUM() over the intersection between each hourly window and each event, right? This might be easiest using tsrange, something like this: SUM(extract(minutes from (tsrange(start_time, end_time) && tsrange(h, h + interval '1 hour'))::interval)) I think you'll have to implement ::interval yourself though, e.g. here: http://dba.stackexchange.com/questions/52153/postgresql-9-2-number-of-days-in-a-tstzrange Also as mentioned you'll have to convert h from an integer [0,23] to a timestamp, but that seems pretty easy. Assuming start_time and end_time are UTC that's just adding that many hours to UTC midnight of the same day. Some weird edge cases to be careful about: activities that cross midnight. Activities that last more than one full day, e.g. start 3/15 and end 3/17. Paul
В списке pgsql-general по дате отправления: