ruby on rails - How to group by hourly or 15 minutes using postgres -
i tried group hourly, failed.
here query
self .where{ name =~ 'memoryinfo' } .where{ sony_alarm_test_id.eq(test_id)} .group("date_part('hour', utc_time )").count
the error message
d, [2014-06-27t15:34:39.713265 #26124] debug -- : (0.6ms) select count(*) count_all, date_part('hour', utc_time ) date_part_hour_utc_time "sony_alarm_logs" "sony_alarm_logs"."sony_alarm_test_id" = 1 , "sony_alarm_logs"."name" ilike 'memoryinfo' group date_part('hour', utc_time ) order utc_time e, [2014-06-27t15:34:39.713421 #26124] error -- : pg::undefinedfunction: error: function date_part(unknown, double precision) not exist line 1: select count(*) count_all, date_part('hour', utc_time ) a...
data
+-------+------------+------------------+--------------------+--------------------------------------------------------+ | id | name | utc_time | sony_alarm_test_id | brief_content | +-------+------------+------------------+--------------------+--------------------------------------------------------+ | 25989 | memoryinfo | 1403512193.57157 | 5 | {"used"=>"156572", "free"=>"150172", "shared"=>"6400"} | | 25990 | memoryinfo | 1403512224.60379 | 5 | {"used"=>"156572", "free"=>"150136", "shared"=>"6436"} | | 25991 | memoryinfo | 1403512255.63598 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 25992 | memoryinfo | 1403512286.66835 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 25993 | memoryinfo | 1403512317.70055 | 5 | {"used"=>"156572", "free"=>"150136", "shared"=>"6436"} | | 25994 | memoryinfo | 1403512348.73276 | 5 | {"used"=>"156572", "free"=>"150136", "shared"=>"6436"} | | 25995 | memoryinfo | 1403512379.76492 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 25996 | memoryinfo | 1403512410.7972 | 5 | {"used"=>"156572", "free"=>"150252", "shared"=>"6320"} | | 25997 | memoryinfo | 1403512441.82937 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 25998 | memoryinfo | 1403512472.86155 | 5 | {"used"=>"156572", "free"=>"150224", "shared"=>"6348"} | | 25999 | memoryinfo | 1403512503.89374 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 26000 | memoryinfo | 1403512534.92593 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 26001 | memoryinfo | 1403512565.95812 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 26002 | memoryinfo | 1403512596.99028 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} | | 26003 | memoryinfo | 1403512628.02261 | 5 | {"used"=>"156572", "free"=>"150144", "shared"=>"6428"} |
there no such function date_part(unknown, double precision)
. documentation date_part
here - can accept timestamp
or interval
. column utc_time
not timestamp
.
you need use to_timestamp()
function in query (docs), like:
self .where{ name =~ 'memoryinfo' } .where{ sony_alarm_test_id.eq(test_id)} .group("date_part('hour', to_timestamp(utc_time) )").count
Comments
Post a Comment