Reputation: 965
this query returns the list below it, however the ids occur multiple times, can i group the ids and add up all the plays so i just get a list unique ids with the total number of plays next to it. is this possible in sql?
SELECT [fkPlayerId],[plays]
FROM [dPlaysPerDay]
INNER JOIN dWinners
ON [dPlaysPerDay].[fkPlayerId]=dWinners.[pkWinnerId]
53693 1
53695 1
53696 1
53693 1
53692 2
53698 2
53697 1
53699 2
53698 3
53693 6
53699 2
53693 2
53700 2
53698 1
Upvotes: 2
Views: 60
Reputation: 103348
When using GROUP BY
, all data items in the SELECT
, must either be included in the GROUP BY
or placed in an Aggregate Function.
Therefore, fkPlayerId
should be grouped by, and plays
used with the SUM()
function
SELECT [fkPlayerId], SUM([plays])
FROM [dPlaysPerDay]
INNER JOIN dWinners ON [dPlaysPerDay].[fkPlayerId]=dWinners.[pkWinnerId]
GROUP BY fkPlayerId
To ORDER BY
the aggregate function, give the data item an alias, and Order by this:
SELECT [fkPlayerId], SUM([plays]) as TotalPlays
FROM [dPlaysPerDay]
INNER JOIN dWinners ON [dPlaysPerDay].[fkPlayerId]=dWinners.[pkWinnerId]
GROUP BY fkPlayerId
ORDER BY TotalPlays
Upvotes: 6