与条件聚合?

时间:2013-12-08 23:35:46

标签: sql postgresql database-schema

我正在尝试聚合,具体取决于条件如果player_id(加里) 得分大于,等于或低于player_id(“其他”)

我的架构有

players(player_id, name) 

matches(match_id, home_team(player_id), away_team(player_id) )

outcome(outcome_id, match_id, home_score:integer, away_score:integer

输出来自:

select m.match_id, p.name AS home_team, p1.name AS away_team, o.home_score, o.away_score
from players p
inner join matches m on (p.player_id = m.home_team)
inner join players p1 on (p1.player_id = m.away_team)
inner join outcomes o on (m.match_id = o.match_id);

 match_id | player_id | player_id | home_score | away_score 
----------+-----------+-----------+------------+------------
        1 | 1         | 2         |          1 |          2
        2 | 2         | 1         |          1 |          3
        3 | 3         | 1         |          3 |          2

通缉输出:

 player_id   | Wins | Draws | Losses
-------------+------+-------+--------
  1          |    1 |    0  |    2
  2    ...   | ...  |    .. |    ...

我的架构可供更改。

编辑(sqlfiddle):http://www.sqlfiddle.com/#!2/7b6c8/1

2 个答案:

答案 0 :(得分:1)

对于没有子查询和联合的解决方案:http://www.sqlfiddle.com/#!2/7b6c8/31

SELECT
    p.player_id,
    COALESCE(SUM(o1.home_score > o1.away_score or o2.home_score < o2.away_score), 0) wins,
    COALESCE(SUM(o1.home_score = o1.away_score or o2.home_score = o2.away_score), 0) draws,
    COALESCE(SUM(o1.home_score < o1.away_score or o2.home_score > o2.away_score), 0) losses
FROM players p
LEFT JOIN matches m1 ON (p.player_id = m1.home_team)
LEFT JOIN players p1 ON (p1.player_id = m1.away_team)
LEFT JOIN outcomes o1 ON (m1.match_id = o1.match_id)
LEFT JOIN matches m2 ON (p.player_id = m2.away_team)
LEFT JOIN players p2 ON (p2.player_id = m2.home_team)
LEFT JOIN outcomes o2 ON (m2.match_id = o2.match_id)

GROUP BY p.player_id    

结果:

PLAYER_ID   WINS    DRAWS   LOSSES
1   1   0   2
2   1   0   1
3   1   0   0
4   0   0   0
5   0   0   0

答案 1 :(得分:1)

我会使用UNION ALL获取两次outcome次,一次用于home,一次用于away玩家。第二次home_score / away_score应该切换,以获得away玩家的正确总和。

select
  d.player_id,
  d.name, 
  sum(d.home_score > d.away_score) as wins,
  sum(d.home_score = d.away_score) as draws,
  sum(d.home_score < d.away_score) as loses
from (
    select p.player_id, p.name, o.home_score, o.away_score
    from players p
    join matches m on p.player_id = m.home_team
    join outcomes o on o.match_id = m.match_id
  union all
    select p.player_id, p.name, o.away_score as home_score, o.home_score as away_score
    from players p
    join matches m on p.player_id = m.away_team
    join outcomes o on o.match_id = m.match_id) d
group by d.player_id, d.name

返回:

PLAYER_ID   NAME    WINS    DRAWS   LOSES
1           Gary    1       0       2
2           Tom     1       0       1
3           Brad    1       0       0

sqlFiddle演示:http://www.sqlfiddle.com/#!2/7b6c8/21