mysql - 如何选择多个 GROUP BY 行的 SUM?

标签 mysql sql

我有一张球员与比赛的关系表以及他们在每场比赛中的得分。我正在尝试做一个 SELECT,在其中我得到一个不同的球员列表,他们的总得分以及他们所参加的所有球队的总得分。由于一切都是关于单人游戏,我不知道如何仅针对该单列退出 GROUP BY 范围。对于下面的例子,我只说每支球队只有两名球员。在实际数据库中,如果重要的话,每个团队都有五个。谢谢大家。

表格“匹配”:

match_id | winning_team |

56427859 |            0 |
56427860 |            1 |
56427861 |            1 |
56427862 |            0 |
56427863 |            1 |

etc...

表“match_players”:

match_id | team | player_id | points |

56427859 |    0 |        10 |      3 |
56427859 |    0 |        33 |      1 |
56427859 |    1 |        26 |      0 |
56427859 |    1 |        39 |      2 |
56427860 |    0 |        23 |      1 |
56427860 |    0 |        33 |      3 |
56427860 |    1 |        18 |      1 |
56427860 |    1 |        10 |      4 |

etc...

期望的结果:

player_id | match_count | total_points | team_total_points | <- This should be
                                                                the total of all
       10 |           2 |            7 |                 9 |    points scored by
       18 |           1 |            1 |                 5 |    the player and
       23 |           1 |            1 |                 4 |    his teammates
       26 |           1 |            0 |                 2 |    in all matches.
       33 |           2 |            4 |                 8 |
       39 |           1 |            2 |                 2 |

查询:

SELECT
    p.player_id,
    COUNT(*) AS match_count,
    SUM(CASE WHEN mp.team = m.winning_team THEN 1 ELSE 0 END) AS win_count,
    SUM(points) AS total_points,
    [________________________________________] AS team_total_points
FROM matches m
INNER JOIN match_players mp ON m.match_id = mp.match_id
INNER JOIN players p ON mp.player_id = p.player_id
GROUP BY player_id
ORDER BY player_id

编辑:

“球队”一栏简单地定义了红色或蓝色、主场或客场等。球员可以在不同的比赛中属于不同的球队。玩家可以在比赛之间交换队伍,例如休息躲避球。

最佳答案

以下查询将计算与每个玩家同队的所有玩家的总分。

SELECT p1.player_id, SUM(p2.total_points) AS team_total_points
FROM match_players AS p1
JOIN (SELECT match_id, team, SUM(points) as total_points
      FROM match_players
      GROUP BY match_id, team) AS p2
ON p1.match_id = p2.match_id AND p1.team = p2.team
GROUP BY p1.player_id

然后您可以将其与原始查询结合起来以添加团队总数。

SELECT
    p.player_id,
    COUNT(*) AS match_count,
    SUM(CASE WHEN mp.team = m.winning_team THEN 1 ELSE 0 END) AS win_count,
    SUM(points) AS total_points,
    mp2.team_total_points
FROM matches m
INNER JOIN match_players mp ON m.match_id = mp.match_id
INNER JOIN players p ON mp.player_id = p.player_id
INNER JOIN 
    (SELECT p1.player_id, SUM(p2.total_points) AS team_total_points
     FROM match_players AS p1
     JOIN (SELECT match_id, team, SUM(points) as total_points
           FROM match_players
           GROUP BY match_id, team) AS p2
     ON p1.match_id = p2.match_id AND p1.team = p2.team
     GROUP BY p1.player_id) AS mp2 ON mp2.player_id = p.player_id
GROUP BY player_id
ORDER BY player_id

DEMO

关于mysql - 如何选择多个 GROUP BY 行的 SUM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36808807/

相关文章:

php - 使用时间戳 MySQL 按月解析 SQL 结果

mysql - 从云服务器改为专用服务器会提高MySQL性能吗?

mysql - 从一个表中选择,从另一个 id 链接的表中计数

php - 使用 PHP 将 SQL 表导出为 CSV 格式

mysql - 使用重复项选择具有最新时间戳MySQL的行

mysql - 改进 MySQL 中的更新

sql - 查询不大于当前日期的多个日期

PHP MySQLi fetch "array push"覆盖数据

mysql - 如何使用 SQL 查询以相同的名称和相同的表和行/内容以不同的名称克隆 MySQL 数据库

mysql - 保存查询结果以在 IN 比较中使用