需要缺席并提供月份名称

时间:2015-07-02 06:48:41

标签: mysql sql select group-by

我需要一个月份名称,缺席和现在的计数。这是我的数据库查询:

SELECT sid,COUNT(CASE WHEN STATUS ='A' THEN 1 END) AS absent_count,COUNT(CASE WHEN STATUS ='P' THEN 1 END) AS present_count,
      MONTHNAME(attendance_date) AS `Month_Name`
      FROM attendance
      WHERE SID = '2'
      AND campus_id = 2
      GROUP BY sid;

1 个答案:

答案 0 :(得分:1)

sid分组中没有任何意义 - 根据您的'2'条款,它始终为where。相反,由于您想要计算每月的名称,这应该出现在group by子句中:

SELECT   MONTHNAME(attendance_date) AS `Month_Name`, 
         COUNT(CASE WHEN STATUS ='A' THEN 1 END) AS absent_count,
         COUNT(CASE WHEN STATUS ='P' THEN 1 END) AS present_count,
FROM     attendance
WHERE    sid = '2' AND campus_id = 2
GROUP BY MONTHNAME(attendance_date);