Rails& Postgresql:如何按小时分组查询? [英] Rails & Postgresql: how to group queries by hour?
本文介绍了Rails& Postgresql:如何按小时分组查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何在Postgres& Rails的?我已经阅读了不少答案,但是我收到了错误。
这适用于按日期分组:
Model.group(date(updated_at))。count
然后,我尝试了以下小时,但它们不起作用:
Model.group(hour( (updated_at))。count。
Model.group(date_format(updated_at,'%H'))。count。
Model.group(extract(hour from updated_at))。count
一旦我找到特定的小时我需要更新,那么我将如何获得那些小时的模型?即:
Model.where(hour(updated_at)=?,5)
$ c $您可以尝试以下内容:
<$ p
解决方案 $ p> Model.group(DATE_PART('hour',updated_at))。count
更新:
如何查找记录
Model.where(DATE_PART('hour',updated_at)=?,5)
How do I group by hour in Postgres & Rails? I've read through quite a few SO answers but I'm getting errors.
This works when grouping by date:
Model.group("date(updated_at)").count
Then I tried the following for hour but they didn't work:
Model.group("hour(updated_at)").count
Model.group("date_format(updated_at, '%H')").count
Model.group("extract(hour from updated_at)").count
Once I've found certain hours I need to update, how would I then get the models with those hours? I.e:
Model.where("hour(updated_at) = ?", 5)
解决方案
You could try the following
Model.group("DATE_PART('hour', updated_at)").count
UPDATE:
How to find records
Model.where("DATE_PART('hour', updated_at) = ?", 5)
这篇关于Rails& Postgresql:如何按小时分组查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文