简体   繁体   English

按日期聚合postgresql行

[英]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. 例如,当开始时间是上午9点然后我有记录: - 从星期一上午9点​​到星期二上午8点(分组在一个记录中) - 从星期二上午9点到星期三上午8点(分组在一个记录中),依此类推。 Thanks, for any ideas. 谢谢,任何想法。

I'm going to assume your hour field is an INT, but regardless you should be able to adapt this... 我将假设你的小时字段是一个INT,但无论你应该能够适应这个......

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. 这主要是在上午9点之前的任何一小时处理前一天的一部分。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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