简体   繁体   中英

Aggregating postgresql rows by date

I have records with values like: date, hour, value. Aggregating whole day is easy, i just group by date. My problem is that i need to aggregate records from whole days with specific start/end time. In example when start time is 9 am then i have records: - from monday 9 am to tuesday 8 am (grouped in one record) - from tuesday 9 am to wednesday 8 am (grouped in one record) and so on. Thanks, for any ideas.

I'm going to assume your hour field is an INT, but regardless you should be able to adapt this...

GROUP BY
  CASE WHEN hour >= 9 THEN date ELSE date - 1 END

This essentially treats any hour before 9am as being part of the previous day.

GROUP BY date_trunc('day',date+(hour-9)*interval '1h')或类似的东西?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM