I’m trying to make the following query work:
SELECT
DATE_FORMAT( date, '%Y %m' ) AS `Month`,
COUNT( schedule_id ) AS `Shifts`,
COUNT(user_id) AS `Users`
FROM
schedule
GROUP BY
`Month`, `Shifts`
It should give a frequency table stating how many users work a certain amount of shifts, per month (e.g. in Dec. there were 10 users working 20 shifts, 12 users working 15 shifts etc).
MySQL can’t group on a COUNT() though, so the query breaks. How can I make this work?
Try this:
Inner query returns month, user and shifts count. In outer query you can group by shifts.