2016-04-23 9 views
5

私はマッチする選手とそれぞれ得点したポイントの関係テーブルを持っています。私は、彼らがしてきたすべてのチームによって得点された合計得点と合計得点で選手の明確なリストを取得するSELECTをしようとしています。すべてが単一のプレイヤーの面で行われているので、私はその単一の列だけのGROUP BYスコープから抜け出す方法を知らない。以下の例では、各チームに2人のプレイヤーしかいないと言っています。実際のデータベースでは、重要であれば各チームに5つあります。みんなありがとう。複数のGROUP BY行のSUMを選択するにはどうすればよいですか?

表 "マッチ":

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 

編集:

「チーム」の欄には、単になどプレイヤーは異なる試合で異なるチームにすることができ、赤や青、自宅や離れて定義します。そして、プレイヤーは、リッジドッジボールのように、試合の間にチームを交換することができます。

+0

どのようにして、 "チーム" を定義していますか? –

+0

申し訳ありません。 2つしかない。チームは基本的に家庭と離れていることを意味します。プレイヤーはいつでもどのチームにいてもかまいません。ドッジボールのように。私は自分の投稿を編集します。 – DaiBu

+0

各プレーヤーのチーム合計を計算するサブクエリを作成します。サブクエリをメインクエリと結合し、合計を空白に入れます。 – Barmar

答えて

2

次のクエリでは、各プレイヤーと同じチームのすべてのプレーヤーのための合計ポイントが計算されます。

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

+0

最初の試合でこの権利を得られたとは信じられません:) – Barmar

+0

うわー、速かったです。ええ、私はまさに今のようなものを書く過程にいました。これまでの問題は、SELECTステートメントの結果に加わることができないことを認識していないことでした。それは素晴らしいです。ありがとう。 – DaiBu

+0

私はちょうどJOINからtotal_pointsを引き出して、別のSUMでそれらを再計算するのではなく、メインのクエリでそれを使うことができるようですね。とにかくMySQLはそれを最適化しますか? – DaiBu

0

はこれを試してみてください:

SELECT player_id, COUNT(*) AS match_count, SUM(points) AS total_points, team_tot_points 
FROM match_players JOIN (SELECT SUM(points) AS team_tot_points, team 
         FROM match_players 
         GROUP BY team) AS team_points 
ON match_players.team = team_points.team 
GROUP BY player_id 
+0

これは正しくないことです。プレーヤーがチーム0にいることもあれば、ホームゲームでも離れたゲームでも、チーム1にいることもあるからです。 – Barmar

+0

@ Barmar - 本当ですが、私はその部分を理解することができます。私はこれを試してみる。ありがとう。 – DaiBu

+0

@DaiBu私の答えを見てください。 – Barmar

関連する問題