MySQL - GROUP BY changes results
Please help me explain why adding a GROUP BY clause changes the query's
results. All else staying the same, removing the GROUP BY clause will
produce a different result than adding the GROUP BY clause.
It would seem to me that a GROUP BY would simply group the results by a
field, not alter the count.
I need to organize the data by table2.name, and get a count for each
SELECT table2.name, COUNT(DISTINCT(op.id))
FROM op INNER JOIN table1 ON table1.EID = op.ID
INNER JOIN table3 ON table3.id = table1.jobid
INNER JOIN table2 ON table2.id = table3.CatID
WHERE op.ActiveStartDate <= NOW()
AND op.ActiveEndDate >= NOW()
GROUP BY table2.name
ORDER BY COUNT(*) DESC;
No comments:
Post a Comment