计数的总和与案例和时间

时间:2016-12-12 10:26:31

标签: mysql

SELECT U.id, MONTHNAME(Meeting.created_at) as MonthName,
       (CASE WHEN count(U.id) = 1 then 1 else 0 end) as newUser,
       (CASE WHEN count(U.id) > 1 then 1 else 0 end) as oldUser
FROM  mms_meetings AS Meeting
JOIN mms_meeting_users AS MU ON MU.meeting_id = Meeting.id
JOIN mms_users AS U ON U.id = MU.user_id
GROUP BY U.id, MONTHNAME(Meeting.created_at)

我的结果是

- id MonthName newUser oldUser

 - 276  August  0   1
 - 334  August  1   0
 - 616  August  1   0
 - 729  August  1   0
 - 887  August  1   0
 - 964  August  1   0
 - 992  August  1   0

现在我想要只有月份名称的新旧记录的总和,所以我将此查询更改为喜欢这个

SELECT MONTHNAME(Meeting.created_at) as MonthName,
sum(CASE WHEN count(U.id) = 1 then 1 else 0 end) as newUser,
sum(CASE WHEN count(U.id) > 1 then 1 else 0 end) as oldUser
FROM  mms_meetings AS Meeting
JOIN mms_meeting_users AS MU ON MU.meeting_id = Meeting.id
JOIN mms_users AS U ON U.id = MU.user_id
GROUP BY MONTHNAME(Meeting.created_at)

显示错误[错误] 1111 - Invalid use of group function

任何人都可以帮助我。 我想要像

这样的结果

我想要结果

- MonthName newUser oldUser

 - August   6   1
 - September 45 4
 - October 3    1

0 个答案:

没有答案