I have this query that works fine. It brings data from two tables, manager_details and users. What I need is to bring the sum of managers score BUT from week 1 to week 7. Week is another field in manager_details table which could have values from 0-x.
SELECT
SUM(mng.score) AS accumulated_score,
mng.manager_id AS manager_id,
users.id,
users.fname,
users.lname
FROM manager_details AS mng
JOIN users
ON users.id = mng.manager_id
GROUP BY manager_id
ORDER BY accumulated_score DESC
LIMIT 5
In order to get score from week 1 to week 7, I have tried to add
SELECT
SUM(mng.score) AS accumulated_score,
mng.manager_id AS manager_id,
users.id,
users.fname,
users.lname
FROM manager_details AS mng
WHERE mng.week >= 1 AND mng.week <= 6 -- this line
JOIN users
ON users.id = mng.manager_id
GROUP BY manager_id
ORDER BY accumulated_score DESC
LIMIT 5
and got this
Error Code : 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ‘join users
on users.id = mng.manager_id GROUP BY manager_id
and also tried
...
HAVING mng.week >= 1 AND mng.week <= 6 -- <--
...
I also put the HAVING clase after GROUP BY, before GROUP BY, at the end and did not work, got mysql error check syntax..etc.
I am doing something wrong, but not sure what is it.
So, What clause to use and where do I put that condition to filter my mng.score and get only scores that lie between weeks 1-7?
1 Answer