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

时间:2013-12-06 16:34:18

标签: sql postgresql timestamp aggregate-functions generate-series

我有一个小问题。我有一个具有这种格式的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

3 个答案:

答案 0 :(得分:7)

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从表格中“time"的最小值到最大值”每5分钟生成一行。

  • LEFT JOIN返回表格,以5分钟的间隔切片数据。小心地包括下边框和排除上边框。

  • 要删除未发生任何事情的5分钟广告位,请使用JOIN代替LEFT JOIN

  • 要让您的网格时间从0:00,5:00开始,向下舍入min("time")中的generate_series()

在这些相关答案中有更多解释:
Group by data intervals
PostgreSQL: running count of rows for a query 'by minute'

除此之外:我不会使用time作为标识符。它是Postgres中的reserved word in standard SQL和函数/类型名称。

答案 1 :(得分:4)

试试这个,它应分组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分钟。我认为这应该有用。

答案 2 :(得分:1)

这个怎么样?

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