smurfsdabomb Posted March 23, 2009 Share Posted March 23, 2009 "Select Username, SUM(Score) FROM PlayerUpdate INNER JOIN Table1 ON Table1.PlayerID = Table2.PlayerID JOIN Table3 ON Table1.LeagueID = Table3.LeagueID WHERE Table1.LeagueID = " & Request.QueryString("titleleague") & " group by Table1.Username" When I remove the group by and Sum part I get each of the results repeated twice presumably because of the table overlap but if I use Distinct Username I get the correct result and even if I use distinct within the SUM(distinct score) I get the correct result (though obviously a score of the same value would be discarded), but when I do the query as above I get a score of 28 instead of 14 because its taking the Score result twice, how do I ensure it is only summing it once? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/150781-sql-query-problem-using-group-by/ Share on other sites More sharing options...
gnawz Posted March 23, 2009 Share Posted March 23, 2009 You could be missing an AND before the second JOIN and should it be JOIN or INNER JOIN? Quote Link to comment https://forums.phpfreaks.com/topic/150781-sql-query-problem-using-group-by/#findComment-792180 Share on other sites More sharing options...
smurfsdabomb Posted March 24, 2009 Author Share Posted March 24, 2009 My bad, that should be Inner Join. Must have got deleted with the copy/paste over. Any ideas whats going wrong? It might be something with the way I'm joining tables, if helps any this is the actual query without psuedonames and the relevant table data types. SQLQuery = "Select FantasyTeamAssignment.Username, SUM(PlayerUpdate.CurrentScore) FROM PlayerUpdate INNER JOIN FantasyTeamAssignment ON FantasyTeamAssignment.PlayerID = PlayerUpdate.PlayerID JOIN LeagueSub ON FantasyTeamAssignment.LeagueID = LeagueSub.LeagueID WHERE FantasyTeamAssignment.LeagueID = " & Request.QueryString("titleleague") & " group by FantasyTeamAssignment.Username" Tables FantasyTeamAssignment - LeagueID (int), Username (char(15)), PlayerID (int) LeagueSub - LeagueID (int), Username (nchar(15)), Active (nchar(10)) PlayerUpdate - PlayerID (int), Value (int), CurrentScore(int) Quote Link to comment https://forums.phpfreaks.com/topic/150781-sql-query-problem-using-group-by/#findComment-792230 Share on other sites More sharing options...
fenway Posted March 27, 2009 Share Posted March 27, 2009 Are these many-to-many relationships? You might actually be "joining" in multiple records. Quote Link to comment https://forums.phpfreaks.com/topic/150781-sql-query-problem-using-group-by/#findComment-795293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.