我之前曾发布过有关此内容的文章,这有助于我获得以下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' ) ;
这给了我以下结果:
fname MONTH( eventDate ) DAY COUNT( * )
Kevin 7 weekday 3
Kevin 7 weekend 1
Missy 7 weekday 3
Missy 7 weekend 1
我在尝试实现以下格式时遇到了一些麻烦:
fname MONTH( eventDate ) Weekday COUNT WEEKEND COUNT
Kevin 7 3 1
Missy 7 3 1
谁能提供帮助?我将不胜感激...
您可以在以下位置查看我的“ user”和“ eventcal”架构:
最佳答案
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);
您要在SELECT中进行汇总(在这种情况下为SUM),然后按希望的方式按GROUP BY汇总(按fname,按MONTH)。
关于sql - MySQL工作日/周末计数-第二部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1191462/