MeChris
MeChris

Reputation: 727

sum two times and group

I have table like this:

| ID | Team   | User    | Try1 | Try2 |  Try3 |
| 1  | Black  | Chris   |  2   |  6   |  4    |
| 2  | Black  | Brian   |  10  |  8   |  10   |
| 3  | Red    | Mark    |  6   |  2   |  8    |
| 4  | Red    | Andrew  |  4   |  10  |  6    |

I needed to count team try points together to get total.

SELECT *, SUM(Try1 + Try2 + Try3) AS total FROM team_pts GROUP BY team ORDER BY total DESC

The question is - how do I output each teams total for each try? Something like this:

| Pos | Team  | Try1 | Try2 | Try3 | Total |
|  1  | Black |  12  |  14  |  14  |  40   |
|  2  | Red   |  10  |  12  |  14  |  36   |

Sorry for my English!

Upvotes: 0

Views: 36

Answers (2)

Piotr Olaszewski
Piotr Olaszewski

Reputation: 6204

You need to sum column before sum total:

SELECT *,
       SUM(Try1),
       SUM(Try2),
       SUM(Try3),
       SUM(Try1 + Try2 + Try3) AS total
FROM team_pts
GROUP BY team
ORDER BY total DESC

Upvotes: 1

a basnet
a basnet

Reputation: 17

SELECT ID,Team,SUM(Try1) as Try1,SUM(Try2) as Try2,SUM(Try3) as Try3,SUM(Try1 + Try2 + Try3) AS Total FROM team_pts GROUP BY team ORDER BY total DESC

Upvotes: 1

Related Questions