Reputation: 20163
I have posted about this before, which helped to give me the following SQL:
SELECT fname, MONTH( eventDate ) , IF( WEEKDAY( eventDate ) <5, 'weekday', 'weekend' ) AS
DAY , COUNT( * )
FROM eventcal AS e
LEFT JOIN users AS u ON e.primary = u.username
GROUP BY fname, MONTH( eventDate ) , IF( WEEKDAY( eventDate ) <5, 'weekday', 'weekend' ) ;
And that gives me the following results:
fname MONTH( eventDate ) DAY COUNT( * )
Kevin 7 weekday 3
Kevin 7 weekend 1
Missy 7 weekday 3
Missy 7 weekend 1
I'm having some trouble trying to achieve the following format:
fname MONTH( eventDate ) Weekday COUNT WEEKEND COUNT
Kevin 7 3 1
Missy 7 3 1
Can anyone offer some help? I would greatly appreciate it...
You can see my schemas for 'user' and 'eventcal' at: MySQL/PHP Algorithm for Weekday/Weekend count (per month)
Upvotes: 0
Views: 2792
Reputation: 29301
SELECT
fname,
MONTH(eventDate),
SUM(IF(WEEKDAY(eventDate) < 5,1,0)) AS WeekdayCount,
SUM(IF(WEEKDAY(eventDate) >= 5,1,0)) AS WeekendCount
FROM eventcal AS e
LEFT JOIN users AS u ON e.primary = u.username
GROUP BY fname, MONTH(eventDate);
You want to do your aggregations (SUM in this case) in the SELECT, and GROUP BY how you want them totaled (by fname, by MONTH).
Upvotes: 2