选择按5分钟周期分组的记录的平均值



我有一个小问题。我有一个PostgreSQL表,格式是

time (datetime)     | players (int) | servers (int)
---------------------------------------------------
2013-12-06 13:40:01 | 80            | 20
2013-12-06 13:41:13 | 78            | 21
etc.

我想按5分钟的时间段对它们进行分组,并将组的平均值作为一个值,因此将有20%的记录,每个记录平均包含约5个数字,时间设置为组中的第一个时间值。我不知道如何在PgSQL中做到这一点。所以结果是:

2013-12-06 13:40:01 | avg of players on :40, :41, :42, :43, :44 | same with servers
2013-12-06 13:45:05 | avg of players on :45, :46, :47, :48, :49 | same with servers
2013-12-06 13:50:09 | avg of players on :50, :51, :52, :53, :54 | same with servers
2013-12-06 13:55:12 | avg of players on :55, :56, :57, :58, :59 | same with servers
SELECT grid.t5
      ,min(t."time") AS min_time
--    ,array_agg(extract(min FROM t."time")) AS 'players_on' -- optional
      ,avg(t.players) AS avg_players
      ,avg(t.servers) AS avg_servers
FROM (
   SELECT generate_series(min("time")
                         ,max("time"), interval '5 min') AS t5
   FROM tbl
   ) grid
LEFT JOIN tbl t ON t."time" >= grid.t5
               AND t."time" <  grid.t5 +  interval '5 min'
GROUP  BY grid.t5
ORDER  BY grid.t5;

解释

  • 子查询grid每5分钟生成一行,从表中"time""的最小值到最大值。

  • LEFT JOIN返回到以5分钟为间隔对数据进行切片的表。小心地包括下边界,排除上边界。

  • 要将5分钟的插槽丢弃到没有发生任何事情的位置,请使用JOIN代替LEFT JOIN

  • 要使网格时间从0:00、5:00等开始,请将generate_series()中的min("time")向下取整。

这些相关答案中的更多解释:
按数据间隔分组
PostgreSQL:运行查询的行数';按分钟';

旁白:我不会使用time作为标识符。它是标准SQL中的保留字,也是Postgres中的函数/类型名称。

试试这个,它应该分组分钟0-4、5-9、10-14等等…

SELECT MIN(time), AVG(Players), AVG(Servers)
FROM MyTable t
GROUP BY date_trunc('hour', time),
    FLOOR(datepart('minute', time)/12)

编辑:先将分组更改为小时,然后更改为分钟的Floor。我认为这应该有效。

这个怎么样?

select datepart('year', time) as StartYear, datepart('month', time) as StartMonth,
    datepart('day', time) as StartDay, datepart('hour', time) as StartHour,
    floor(datepart('minute', time)/5)*5 as StartMinute,
    avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5 then players else null end) as Zero,
    avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+1 then players else null end) as One,
    avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+2 then players else null end) as Two,
    avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+3 then players else null end) as Three,
    avg(case when datepart('minute', time) = floor(datepart('minute', time)/5)*5+4 then players else null end) as Four,
from MyTable
group by datepart('year', time), datepart('month', time),
    datepart('day', time), datepart('hour', time),
    floor(datepart('minute', time)/5)*5

相关内容

  • 没有找到相关文章

最新更新