如何简化此游戏统计查询?

时间:2014-11-25 20:39:27

标签: sql postgresql aggregate-functions aggregate-filter

此代码按预期工作,但我很长而且令人毛骨悚然。

select p.name, p.played, w.won, l.lost from

(select users.name, count(games.name) as played
from users
inner join games on games.player_1_id = users.id
where games.winner_id > 0
group by users.name
union
select users.name, count(games.name) as played
from users
inner join games on games.player_2_id = users.id
where games.winner_id > 0
group by users.name) as p

inner join

(select users.name, count(games.name) as won
from users
inner join games on games.player_1_id = users.id
where games.winner_id = users.id
group by users.name
union
select users.name, count(games.name) as won
from users
inner join games on games.player_2_id = users.id
where games.winner_id = users.id
group by users.name) as w on p.name = w.name

inner join

(select users.name, count(games.name) as lost
from users
inner join games on games.player_1_id = users.id
where games.winner_id != users.id
group by users.name
union
select users.name, count(games.name) as lost
from users
inner join games on games.player_2_id = users.id
where games.winner_id != users.id
group by users.name) as l on l.name = p.name

如您所见,它由3个重复部分组成,用于检索:

  • 玩家姓名和他们玩的游戏数量
  • 玩家名称和他们赢得的游戏数量
  • 玩家姓名和他们失去的游戏数量

每个部分也由两部分组成:

  • 玩家姓名以及他们作为player_1参与的游戏数量
  • 玩家姓名及他们作为player_2参与的游戏数量

如何简化?

结果如下:

           name            | played | won | lost 
---------------------------+--------+-----+------
 player_a                  |      5 |   2 |    3
 player_b                  |      3 |   2 |    1
 player_c                  |      2 |   1 |    1

3 个答案:

答案 0 :(得分:3)

由于这是对“漫长而令人毛骨悚然”的追求,因此查询可以大大缩短。即使在pg 9.3 (或实际任何版本)中:

SELECT u.name
     , count(g.winner_id  > 0 OR NULL) AS played
     , count(g.winner_id  = u.id OR NULL) AS won
     , count(g.winner_id <> u.id OR NULL) AS lost
FROM   games g
JOIN   users u ON u.id IN (g.player_1_id, g.player_2_id)
GROUP  BY u.name;

更多解释:

在pg 9.4 中,使用新的聚合FILTER子句(比如@Joe已经提到过)可以更清晰。

SELECT u.name
     , count(*) FILTER (WHERE g.winner_id  > 0) AS played
     , count(*) FILTER (WHERE g.winner_id  = u.id) AS won
     , count(*) FILTER (WHERE g.winner_id <> u.id) AS lost
FROM   games g
JOIN   users u ON u.id IN (g.player_1_id, g.player_2_id)
GROUP  BY u.name;

答案 1 :(得分:1)

这是相关子查询可以简化逻辑的情况:

select u.*, (played - won) as lost
from (select u.*,
             (select count(*)
              from games g
              where g.player_1_id = u.id or g.player_2_id = u.id
             ) as played,
             (select count(*)
              from games g
              where g.winner_id = u.id
             ) as won
      from users u
     ) u;

这假设没有联系。

答案 2 :(得分:1)

select users.name, 
       count(case when games.winner_id > 0 
                  then games.name 
                  else null end) as played,
       count(case when games.winner_id = users.id 
                  then games.name 
                  else null end) as won,
       count(case when games.winner_id != users.id 
                  then games.name 
                  else null end) as lost
from users inner join games 
     on games.player_1_id = users.id or games.player_2_id = users.id
group by users.name;