SELECT
players.id,
players.name,
games.season_id,
games.game_type_id,
SUM(COALESCE(assists.rows, 0)) AS assists,
SUM(COALESCE(goals.rows, 0)) AS goals
FROM
players
CROSS JOIN
games
LEFT JOIN
(SELECT game_id, player_id, COUNT(*) AS rows FROM assists GROUP BY game_id, player_id) AS assists
ON assists.game_id = games.game_id
LEFT JOIN
(SELECT game_id, player_id, COUNT(*) AS rows FROM goals GROUP BY game_id, player_id) AS assists
ON goals.game_id = games.game_id
GROUP BY
players.id,
players.name,
games.season_id,
games.game_type_id
Seasons
ただし、テーブルとテーブルがあれば、パフォーマンスが向上する可能性がありGameType
ます。
SELECT
players.id,
players.name,
seasons.id,
game_types.id,
SUM(COALESCE(assists.rows, 0)) AS assists,
SUM(COALESCE(goals.rows, 0)) AS goals
FROM
players
CROSS JOIN
seasons
CROSS JOIN
game_types
LEFT JOIN
games
ON games.season_id = seasons.id
AND games.game_type = game_types.id
LEFT JOIN
(SELECT game_id, player_id, COUNT(*) AS rows FROM assists GROUP BY game_id, player_id) AS assists
ON assists.game_id = games.game_id
LEFT JOIN
(SELECT game_id, player_id, COUNT(*) AS rows FROM goals GROUP BY game_id, player_id) AS assists
ON goals.game_id = games.game_id
GROUP BY
players.id,
players.name,
seasons.id,
game_types.id