复杂的总和与分组

时间:2010-06-22 14:22:29

标签: sql sql-server-2008

我有一个名为Games with table的列: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)中实现这一目标?如果我需要一点积分怎么办? 只有当一些球员赢了比赛?可以在不使用变量,循环等的情况下完成吗?

5 个答案:

答案 0 :(得分:3)

我很想重新组织数据,或许:

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

答案 1 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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

根据您想要处理关系的方式,您需要更改> =。

答案 4 :(得分: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;