Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select Number of Match played/Player Id and sum(Goal)

Match Id Player Id Goal
1 119 1
1 120 1
1 142 1
1 119 0
1 120 1
1 137 1
2 119 1
2 120 1
3 119 1

What I have tried:

SELECT tmp.[Player Id], Count(tmp.[Match Id]) AS MatchPlayed
FROM (SELECT DISTINCT
          [Match Id],[Player Id]
      FROM [Match Details]
    )  AS tmp
GROUP BY tmp.[Player Id];
Posted
Updated 2-Mar-17 4:59am

Preliminary note: on MS SQL Server you can use: SELECT COUNT(DISTINCT <FieldName>) FROM ..., but on MS Access database such as statement will cause an error. ;(

So, you can choose one of the way:
1) subquery
2) joined table

This should work as expected:
SQL
SELECT tmp.[Player Id], SUM(tmp.[MatchPlayed) AS MatchPlayed, SUM(t2.Goal) AS PointsReached
FROM (SELECT DISTINCT [Player Id], COUNT([Match Id]) As MatchPlayed
      FROM [Match Details]
      GROUP BY [Player Id]
    ) AS tmp INNER JOIN [Match Details] AS t2 ON tmp.[Player Id] = t2.[Player Id]
GROUP BY tmp.[Player Id];


For further details, please see: Access Subquery Techniques[^]
Tips and Techniques for Queries in Access 2007[^]
 
Share this answer
 
Comments
Karthik_Mahalingam 2-Mar-17 22:34pm    
5 for the link
Maciej Los 3-Mar-17 13:08pm    
Thank you, Karthik.
Untested but something like

SELECT [Player Id], Count([Match Id]) AS MatchPlayed, Sum([Goal]) AS GoalCount
FROM [Match Details] GROUP BY [Player Id]
 
Share this answer
 
Comments
Maciej Los 2-Mar-17 10:42am    
This won't return distinct MatchId ;(
F-ES Sitecore 2-Mar-17 10:53am    
If a player has 3 matches and 10 goals, which Match ID are you looking to return? If you want all the matches played by that person as well then it'll be easier to simply execute two queries, one to get the stats and one to list the matches.
Maciej Los 2-Mar-17 11:02am    
Take a look at OP's data:
1 119 1
...
1 119 0

Player 119 played 2 matches with the same ID. So, your query will return 2 matches, but OP wants to return only 1. Got it?
F-ES Sitecore 2-Mar-17 11:38am    
If he played two matches the match count is two, that's correct as far as I can see.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900