[英]How to group by hourly or 15 minutes using postgres
I tried to group by hourly, but failed. 我试图按小时分组,但失败了。
Here my 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(*) AS count_all, DATE_PART('hour', utc_time ) AS date_part_hour_utc_time FROM "sony_alarm_logs" WHERE "sony_alarm_logs"."sony_alarm_test_id" = 1 AND "sony_alarm_logs"."name" ILIKE 'MemoryInfo' GROUP BY DATE_PART('hour', utc_time ) ORDER BY utc_time
E, [2014-06-27T15:34:39.713421 #26124] ERROR -- : PG::UndefinedFunction: ERROR: function date_part(unknown, double precision) does not exist
LINE 1: SELECT COUNT(*) AS 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 is no such function date_part(unknown, double precision)
. 没有这样的函数date_part(unknown, double precision)
。 Documentation for date_part
is here - it can accept timestamp
or interval
. date_part
文档在这里 -它可以接受timestamp
或interval
。 Column utc_time
is not timestamp
. 列utc_time
不是timestamp
。
You will need to use to_timestamp()
function in your query ( docs ), something like: 您将需要在查询( docs )中使用to_timestamp()
函数,例如:
self
.where{ name =~ 'MemoryInfo' }
.where{ sony_alarm_test_id.eq(test_id)}
.group("DATE_PART('hour', to_timestamp(utc_time) )").count
Your utc_time
column is double precision
. 您的utc_time
列是double precision
。 All date/time functions works on timestamp[tz]
, date
and time[tz]
types. 所有日期/时间函数都适用于timestamp[tz]
, date
和time[tz]
类型。
You can convert your double precision
fields to timestamp
using the function to_timestamp(double precision)
-- assuming utc_time
counts seconds (and not milliseconds) from the unix epoch. 您可以使用函数to_timestamp(double precision)
将double precision
字段转换为timestamp
-假设utc_time
从unix纪元utc_time
是秒(而不是毫秒)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.