jp1989 Posted July 21, 2013 Share Posted July 21, 2013 Hi all. I am having trouble building my SQL query that attempts to get the SUM of all points for 3 types of activities - for each user. My query is below and here is an image that explains what I'm trying to do. I am quite close, and I kind of know what's going wrong, but would love some help! http://imgur.com/Lyjh7UD [1] (my problem and what I'd like). Query: SELECT *FROM(SELECT userid, SUM(points) AS totalPoints FROM entriesWHERE competitionId = '$competitionId'GROUP BY userid)x, (SELECT userid, activity_id, SUM(points) AS points1FROM entriesWHERE competitionId = '$competitionId'AND activity_id = '1'GROUP BY activity_id)a, (SELECT userid, activity_id, SUM(points) AS points2FROM entriesWHERE competitionId = '$competitionId'AND activity_id = '2'GROUP BY activity_id)b, (SELECT userid, activity_id, SUM(points) AS points3FROM entriesWHERE competitionId = '$competitionId'AND activity_id = '3'GROUP BY activity_id)c Link to comment https://forums.phpfreaks.com/topic/280362-help-with-sql-query-with-3-where-sum-statements/ Share on other sites More sharing options...
jp1989 Posted July 21, 2013 Author Share Posted July 21, 2013 Solved. SELECT userid, SUM(points) AS totalPoints, SUM(CASE WHEN activity_id = 1THEN pointsELSE NULL END) AS points1, SUM(CASE WHEN activity_id = 2THEN pointsELSE NULL END) AS points2, SUM(CASE WHEN activity_id = 3THEN pointsELSE NULL END) AS points3FROM entriesWHERE competitionId = '$competitionId'GROUPBY userid Link to comment https://forums.phpfreaks.com/topic/280362-help-with-sql-query-with-3-where-sum-statements/#findComment-1441600 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.