计算平均时间间隔长度

时间:2018-04-23 14:50:32

标签: sql postgresql average moving-average postgresql-10

我准备了一个简单的SQL Fiddle来证明我的问题 -

在PostgreSQL 10.3中,我将用户信息,双人游戏以及移动存储在以下3个表中:

CREATE TABLE players (
    uid SERIAL PRIMARY KEY,
    name text NOT NULL
);

CREATE TABLE games (
    gid SERIAL PRIMARY KEY,
    player1 integer NOT NULL REFERENCES players ON DELETE CASCADE,
    player2 integer NOT NULL REFERENCES players ON DELETE CASCADE
);

CREATE TABLE moves (
    mid BIGSERIAL PRIMARY KEY,
    uid integer NOT NULL REFERENCES players ON DELETE CASCADE,
    gid integer NOT NULL REFERENCES games ON DELETE CASCADE,
    played timestamptz NOT NULL
);

让我们假设2名球员,爱丽丝和鲍勃互相打了3场比赛:

INSERT INTO players (name) VALUES ('Alice'), ('Bob');
INSERT INTO games (player1, player2) VALUES (1, 2);
INSERT INTO games (player1, player2) VALUES (1, 2);
INSERT INTO games (player1, player2) VALUES (1, 2);

让我们假设第一场比赛很快就开始了,每分钟都会进行一次动作。

然后他们冷静:-)并且每2分钟进行2次慢速比赛:

INSERT INTO moves (uid, gid, played) VALUES
(1, 1, now() + interval '1 min'),
(2, 1, now() + interval '2 min'),
(1, 1, now() + interval '3 min'),
(2, 1, now() + interval '4 min'),
(1, 1, now() + interval '5 min'),
(2, 1, now() + interval '6 min'),

(1, 2, now() + interval '10 min'),
(2, 2, now() + interval '20 min'),
(1, 2, now() + interval '30 min'),
(2, 2, now() + interval '40 min'),
(1, 2, now() + interval '50 min'),
(2, 2, now() + interval '60 min'),

(1, 3, now() + interval '110 min'),
(2, 3, now() + interval '120 min'),
(1, 3, now() + interval '130 min'),
(2, 3, now() + interval '140 min'),
(1, 3, now() + interval '150 min'),
(2, 3, now() + interval '160 min');

在一个有游戏统计数据的网页上,我想显示每个玩家之间的平均移动时间。

所以我想我必须使用PostgreSQL的LAG window function

由于可以同时播放几个游戏,我正在尝试PARTITION BY gid(即通过“游戏ID”)。

不幸的是,我得到了一个语法错误窗口函数调用无法与我的SQL查询嵌套

SELECT AVG(played - LAG(played) OVER (PARTITION BY gid order by played))
OVER (PARTITION BY gid order by played)
FROM moves
-- trying to calculate average thinking time for player Alice
WHERE uid = 1;

更新

由于我的数据库中的游戏数量很大而且日益增长,我尝试(这里是新的SQL Fiddle)为内部选择查询添加条件:

SELECT AVG(played - prev_played)
FROM (SELECT m.*,
      LAG(m.played) OVER (PARTITION BY m.gid ORDER BY played) AS prev_played
      FROM moves m
      JOIN games g ON (m.uid in (g.player1, g.player2))
      WHERE m.played > now() - interval '1 month'
     ) m
WHERE uid = 1;

但是由于某种原因,这会将返回值彻底改变为1分45秒。

我想知道,为什么内部SELECT查询突然返回更多行,可能在我的JOIN中缺少一些条件?

更新2:

哦,好吧,我明白了平均值减少的原因:通过多个具有相同时间戳的行(即played - prev_played = 0),但是如何修复JOIN?

更新3:

没关系,我在SQL JOIN now it works中错过了m.gid = g.gid AND条件:

SELECT AVG(played - prev_played)
FROM (SELECT m.*,
      LAG(m.played) OVER (PARTITION BY m.gid ORDER BY played) AS prev_played
      FROM moves m
      JOIN games g ON (m.gid = g.gid AND m.uid in (g.player1, g.player2))
      WHERE m.played > now() - interval '1 month'
     ) m
WHERE uid = 1;

2 个答案:

答案 0 :(得分:2)

您需要子查询来嵌套窗口函数。我认为这样做你想要的:

select avg(played - prev_played)
from (select m.*,
             lag(m.played) over (partition by gid order by played) as prev_played
      from moves m
     ) m
where uid = 1;

注意:where需要进入外部查询,因此不会影响lag()

答案 1 :(得分:1)

可能@gordon的答案已经足够了。但这不是您在评论中提出的结果。仅适用,因为每个游戏的数据行数相同,因此游戏的平均值与完全平均值相同。但如果你想要平均的游戏,你需要一个额外的水平。

With cte as (
    SELECT gid, AVG(played - prev_played) as play_avg
    FROM (select m.*,
                 lag(m.played) over (partition by gid order by played) as prev_played
          from moves m      
         ) m
    WHERE uid = 1
    GROUP BY gid
)
   SELECT AVG(play_avg)
   FROM cte
;