按日期聚合 postgresql 行
我的记录的值如下:日期、小时、值。汇总一整天很容易,我只需按日期分组即可。我的问题是我需要汇总具有特定开始/结束时间的全天记录。例如,当开始时间为上午 9 点时,我有记录: - 从周一上午 9 点到周二上午 8 点(分组为一条记录) - 从周二上午 9 点到周三上午 8 点(分组为一条记录) 等等。 谢谢,任何想法。
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 a.m. then i have records:
- from monday 9 a.m. to tuesday 8 a.m (grouped in one record)
- from tuesday 9 a.m. to wednesday 8 a.m. (grouped in one record)
and so on.
Thanks, for any ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设你的小时字段是 INT,但无论如何你应该能够适应这一点......
这本质上将上午 9 点之前的任何小时视为前一天的一部分。
I'm going to assume your hour field is an INT, but regardless you should be able to adapt this...
This essentially treats any hour before 9am as being part of the previous day.
GROUP BY date_trunc('day',date+(hour-9)*interval '1h')
或类似的东西?GROUP BY date_trunc('day',date+(hour-9)*interval '1h')
or something like that?