Grouped compound amount

I have a table called Games with Columns: Player1Id, Player2Id, Player1Points, Player2Points.

Sample data:

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

And I want to have a list with the sum of points for each player, for example:

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

How to achieve this in SQL (SQL Server 2008)? What if I need a sum of points? only if some player won the match? Is it possible to do this without using variables, loops, etc.?

+3
source share
5 answers

I will be tempted to reorganize the data, perhaps:

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

etc.

+3
source
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

Productivity:

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

This will only be considered victories:

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
source

To answer the second question, you could change Marcus's answer to this:

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

Depending on how you want to handle the links, you will need to change β†’.

+1
source

Test Tables:

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;

Points Amount:

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;

Total points from games won:

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;

Difference:

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;
0
source

Source: https://habr.com/ru/post/1751218/


All Articles