1

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

3 回答 3

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每 5 分钟生成一行time"

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

  • 要删除没有发生任何事情的 5 分钟槽,请使用JOIN.LEFT JOIN

  • 要让您的网格时间从 0:00、5:00 等开始,请将 in 向下舍min("time")generate_series()

这些相关答案中的更多解释:
按数据间隔分组
PostgreSQL: running count of rows for a query 'by minute'

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

于 2013-12-06T17:12:47.670 回答
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为分钟。我认为这应该有效。

于 2013-12-06T17:07:57.933 回答
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
于 2013-12-06T16:45:15.940 回答