2010-06-22 70 views
3

我有表名爲遊戲與列:Player1Id,Player2Id,Player1Points,Player2Points。與分組的複雜的總和

的樣本數據:

Player1Id Player2Id Player1Points Player2Points 
--------- --------- ------------- ------------- 
John   Piter  4     1 
John   Adam   2    10 
Piter  Adam   4     2 

而且我想與點的總和列表每個球員,這樣的:

PlayerId Points 
-------- ------ 
John   6 
Piter  5 
Adam  12 

如何才達到,在SQL(SQL Server 2008中) ?如果只有某位球員贏得比賽,我需要總積分 ?它可以不使用變量,循環等?

回答

2
SELECT Player1Id AS PlayerId, SUM(Player1Points) AS Points FROM 
(SELECT Player1Id, Player1Points FROM MyTable 
UNION ALL 
SELECT Player2Id, Player2Points FROM MyTable) t1 
GROUP BY Player1Id 
ORDER BY Player1Id 

產量:

PlayerId  Points 
--------  ------ 
Adam    12 
John    6 
Piter    5 

如果你添加更多的玩家查詢將獲得更加複雜。最好在不同的行上記錄每個玩家的分數。你可以添加一列來指定遊戲。

PlayerId Points GameId 
-------- ------ ------ 
John   4  1 
Piter   1  1 
John   2  2 
Adam   10  2 
Piter   4  3 
Adam   2  3 

您的查詢將被簡單:

SELECT PlayerId, SUM(Points) 
FROM MyTable 
GROUP BY PlayerId 
ORDER BY PlayerId 
+0

這應該是'UNION ALL'(如果約翰在兩場比賽中得分3,它只會計算一次) – Quassnoi 2010-06-22 14:31:19

+0

@Quassnoi,修正了,謝謝! – 2010-06-22 14:33:27

3

我會忍不住重新組織數據,也許是:

Gameid Player Score 
    1 John  4 
    2 John  2 
    1 Piter 1 

+0

另外,如果您開始與3名玩家進行遊戲,則不必添加列。 + 1用於建議標準化數據。 – HLGEM 2010-06-22 14:42:09

0

測試表:

DECLARE @Table TABLE (Player1Id VARCHAR(17), Player2Id VARCHAR(17), Player1Points INT, Player2Points INT); 
INSERT INTO @Table 
    SELECT 'John', 'Piter', 4, 1 UNION ALL 
    SELECT 'John', 'Adam', 2, 10 UNION ALL 
    SELECT 'Piter', 'Adam', 4, 2; 

總積分:從贏得比賽

WITH CTE (PlayerId, Points) AS (
    SELECT Player1Id, Player1Points FROM @Table 
    UNION ALL 
    SELECT Player2Id, Player2Points FROM @Table 
) 
SELECT PlayerId, SUM(Points) FROM CTE GROUP BY PlayerId; 

總積分:

WITH CTE (PlayerId, Points, Won) AS (
    SELECT Player1Id, Player1Points, CASE WHEN Player1Points > Player2Points THEN 1 ELSE 0 END FROM @Table 
    UNION ALL 
    SELECT Player2Id, Player2Points, CASE WHEN Player2Points > Player1Points THEN 1 ELSE 0 END FROM @Table 
) 
SELECT PlayerId, SUM(Points) FROM CTE WHERE Won = 1 GROUP BY PlayerId; 

差異:

WITH CTE (PlayerId, Points) AS (
    SELECT Player1Id, CASE WHEN Player1Points > Player2Points THEN Player1Points - Player2Points ELSE (Player2Points - Player1Points) * -1 END FROM @Table 
    UNION ALL 
    SELECT Player2Id, CASE WHEN Player2Points > Player1Points THEN Player2Points - Player1Points ELSE (Player1Points - Player2Points) * -1 END FROM @Table 
) 
SELECT PlayerId, SUM(Points) FROM CTE GROUP BY PlayerId; 
1

這隻會計算分數的勝利:

SELECT player, SUM(score) AS score 
FROM (
     SELECT player1id AS player, player1points AS score 
     FROM mytable 
     WHERE player1points > player2points 
     UNION ALL 
     SELECT player2id, player2points 
     FROM mytable 
     WHERE player2points > player1points 
     ) q 
GROUP BY 
     player 
1

要回答你的第二個問題,然後你可以改變馬庫斯的答案是:

SELECT 
    Player1Id, 
    SUM(Player1Points) 
FROM 
(
    SELECT Player1Id, Player1Points FROM MyTable WHERE Player1Points >= Player2Points 
    UNION ALL 
    SELECT Player2Id, Player2Points FROM MyTable WHERE Player2Points >= Player1Points 
) t1 
GROUP BY 
    Player1Id 

根據您想如何處理的關係,你需要改變> =。