开发者

Problem with GROUP BY statement (SQL)

开发者 https://www.devze.com 2023-02-12 21:56 出处:网络
I have a table GAMES with this information: Id_GameId_Player1Id_Player2Week --------------------------------------

I have a table GAMES with this information:

Id_Game  Id_Player1  Id_Player2  Week
--------------------------------------
1211     Peter       John        2
1215     John        Louis       13
1216     Louis       Peter       17

I would like to get a list of the last week when each player has played, and the number of games, which should be this:

Id_P开发者_C百科layer  Week  numberGames
-----------------------------
Peter      17    2
John       13    2
Louis      17    2

But instead I get this one (notice on Peter week):

Id_Player  Week  numberGames
-----------------------------
Peter      2     2
John       13    2
Louis      17    2

What I do is this:

  SELECT Id_Player, 
         MAX(Week) AS Week, 
         COUNT(*) as numberGames 
    FROM ((SELECT Id_Player1 as Id_Player, Week 
             FROM Games) 
           UNION ALL
           (SELECT Id_Player2 as Id_Player, Week 
              FROM Games)) AS g2 
GROUP BY Id_Player;

Could anyone help me to find the mistake?


What is the datatype of the Week column? If the datatype of Week is varchar you would get this behavior.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号