2013-11-14 57 views
2

我的數據庫表::如何編寫查詢來獲取月份的數據?

attendence date  admission number   attendence 

    2013-10-2   LSTM-0008/2013-2014  present 

2013-10-19   LSTM-0008/2013-2014  absent 

    2013-9-20   LSTM-0008/2013-2014  present 

上面一個是我的數據庫表。

,我想這樣的基於數據庫的表中顯示錶:

MonthName  totalWorkingDays  Present  absent 

october   26     1   1 
november   26     1   0 

我寫MySQL查詢像這樣:

SELECT DISTINCT monthname(attendencedate)as monthname , COUNT (*) as totalworking days, 
    (SELECT COUNT(*) FROM lstms_attendence WHERE attendence='present' AND addmissionno='LSTM-0008/2013-2014') as present, 
    (SELECT COUNT(*) FROM lstms_attendence WHERE attendence='absent' AND addmissionno='LSTM-0008/2013-2014') as absent 
FROM lstms_attendence 
WHERE addmissionno='LSTM-0008/2013-2014' 
GROUP BY attendencedate; 

它不是爲我工作的任何人給我建議。

回答

2

嘗試這種情況:

SELECT monthname(attendencedate) AS monthname, 
    COUNT(*) AS totalworking_days, 
    SUM(CASE WHEN attendence = 'present' THEN 1 ELSE 0 END) AS present, 
    SUM(CASE WHEN attendence = 'absent' THEN 1 ELSE 0 END AS absent  
FROM lstms_attendence 
WHERE addmissionno = 'LSTM-0008/2013-2014' 
GROUP BY monthname(attendencedate); 

它將SUM 1用於本塔具有考勤每一行=「存在」,否則爲0。同樣的文憑='缺席'

+0

Thanku它爲我工作。 –

相關問題