如何获得整个月的员工出勤总和

时间:2014-12-10 14:35:51

标签: mysql

我正在研究考勤系统,我有表tableattendance字段     EMP_ID,     DATE_TIME_IN,     DATE_TIME_OUT, ATTANDANCE_STATUS,

当员工准时打电话时,对于后来者来说,它会被视为“P”和“Y”。现在我有一个查询来获取完整的月份出勤记录。

MySQL查询

SELECT  EMP_ID,
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-01' THEN ATTENDANCE_STATUS END) '2014-12-01',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-02' THEN ATTENDANCE_STATUS END) '2014-12-02',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-03' THEN ATTENDANCE_STATUS END) '2014-12-03',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-04' THEN ATTENDANCE_STATUS END) '2014-12-04',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-05' THEN ATTENDANCE_STATUS END) '2014-12-05',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-06' THEN ATTENDANCE_STATUS END) '2014-12-06',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-07' THEN ATTENDANCE_STATUS END) '2014-12-07',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-08' THEN ATTENDANCE_STATUS END) '2014-12-08',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-09' THEN ATTENDANCE_STATUS END) '2014-12-09',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-10' THEN ATTENDANCE_STATUS END) '2014-12-10',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-11' THEN ATTENDANCE_STATUS END) '2014-12-11',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-12' THEN ATTENDANCE_STATUS END) '2014-12-12',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-13' THEN ATTENDANCE_STATUS END) '2014-12-13',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-14' THEN ATTENDANCE_STATUS END) '2014-12-14',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-15' THEN ATTENDANCE_STATUS END) '2014-12-15',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-16' THEN ATTENDANCE_STATUS END) '2014-12-16',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-17' THEN ATTENDANCE_STATUS END) '2014-12-17',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-18' THEN ATTENDANCE_STATUS END) '2014-12-18',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-19' THEN ATTENDANCE_STATUS END) '2014-12-19',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-20' THEN ATTENDANCE_STATUS END) '2014-12-20',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-21' THEN ATTENDANCE_STATUS END) '2014-12-21',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-22' THEN ATTENDANCE_STATUS END) '2014-12-22',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-23' THEN ATTENDANCE_STATUS END) '2014-12-23',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-24' THEN ATTENDANCE_STATUS END) '2014-12-24',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-25' THEN ATTENDANCE_STATUS END) '2014-12-25',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-26' THEN ATTENDANCE_STATUS END) '2014-12-26',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-27' THEN ATTENDANCE_STATUS END) '2014-12-27',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-28' THEN ATTENDANCE_STATUS END) '2014-12-28',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-29' THEN ATTENDANCE_STATUS END) '2014-12-29',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-30' THEN ATTENDANCE_STATUS END) '2014-12-30',
    MAX(CASE WHEN DATE_FORMAT(DATE_TIME_IN, '%Y-%m-%d') = '2014-12-31' THEN ATTENDANCE_STATUS END) '2014-12-31'
FROM `tblattendance`
GROUP BY EMP_ID;

此查询非常正常,可生成以下结果。

结果

EMP_ID 2014-12-01  2014-12-02  2014-12-03  2014-12-04  2014-12-05  2014-12-06  2014-12-07  2014-12-08  2014-12-09  2014-12-10  2014-12-11  2014-12-12  2014-12-13  2014-12-14  2014-12-15  2014-12-16  2014-12-17  2014-12-18  2014-12-19  2014-12-20  2014-12-21  2014-12-22  2014-12-23  2014-12-24  2014-12-25  2014-12-26  2014-12-27  2014-12-28  2014-12-29  2014-12-30  2014-12-31  

 1  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 2  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 3  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 4  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 5  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 6  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 7  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 8  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           
 9  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           Y           P           Y           P           P           Y           P           P           Y           P           P           Y           P           
10  P           P           P           Y           P           P           P           P           P           Y           P           P           P           P           P           P           P           P           Y           P           Y           P           P           Y           P           P           Y           P           P           Y           P           

目标

现在,我的目标是计算礼品总数,并在结果结束时为每位员工延迟。请帮助任何人。

0 个答案:

没有答案