Ruby On Rails,Postgresql - 每个分钟组的Date_Part或Extract

时间:2015-01-05 17:21:00

标签: ruby-on-rails postgresql group-by

我正在寻找一种方法来获取每分钟记录的分组输出。我正在寻找一个看起来像这样的结果:

{"2015-01-05 12:05" => 5, "2015-01-05 12:06" => 2}

使用DATE_PARTEXTRACT时,我会将其作为结果

{0.0=>1, 48.0=>2, 23.0=>3, 42.0=>2, 47.0=>2, 54.0=>1, 46.0=>2, 2.0=>1, 44.0=>2, 45.0=>2, 53.0=>1, 6.0=>3, 49.0=>1, 56.0=>2, 22.0=>1, 51.0=>1, 43.0=>2, 24.0=>2, 5.0=>1, 7.0=>1, 37.0=>2, 33.0=>2, 50.0=>1, 1.0=>2}

这是我的代码:

Model.group("DATE_PART('minute', created_at)").count
Model.group("EXTRACT(MINUTE FROM created_at)").count

1 个答案:

答案 0 :(得分:2)

Model.group("to_char(created_at,'YYYY-MM-DD HH24:MI')").count