使用mysql计算动态日出勤率

s71maibg  于 2023-03-17  发布在  Mysql
关注(0)|答案(1)|浏览(161)

我正在创建一个网络应用程序,以生成每个员工的日常出勤,他们可以打卡/下班。
表1 -雇员名单

| userID | Fullname   | Employee ID |
| 1      | John Doe   | ABC-0001    |
| 2      | Jeffrey Doe| ABC-0002    |

表2 -出席情况

| attendanceID | userID | Date       | Time                | Remarks |
| 1            | 1      | 2023-01-01 | 2023-01-01 07:00:00 | IN      |
| 2            | 2      | 2023-01-01 | 2023-01-01 07:30:25 | IN      |
| 3            | 1      | 2023-01-01 | 2023-01-01 17:01:25 | OUT     |
| 4            | 2      | 2023-01-01 | 2023-01-01 18:05:00 | OUT     |

我试着用这段代码来生成考勤水平视图.

SET @sql = NULL;

SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      '(CASE WHEN Date = ',
      Date,
      ' THEN TIMESTAMPDIFF(HOUR,SUM(CASE WHEN Remarks="IN" THEN TIME END),SUM(CASE WHEN Remarks="OUT" THEN TIME END)) END) AS ',
      CONCAT('"',Date,'"')
    )
  ) INTO @sql
FROM tblattendance;

SET @sql =
  CONCAT('SELECT a.Fullname,a.EmployeeID, ', @sql, ' FROM tblemployee a LEFT JOIN tblattendance b ON b.eID = a.eID GROUP BY a.userID');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

但我收到了一些警告,如:

Warning: #1292 Truncated incorrect DECIMAL value: '2023-03-10'

| John Doe | ABC-0001 | NULL |

希望你能帮我解决这个问题。
下面是我希望生成的输出

| Fullname      | EmployeeID | 2023-01-01 |
| John Doe      | ABC-0001   | 9          |
| Jeffrey Doe   | ABC-0002   | 9          |
vltsax25

vltsax251#

在示例数据中有Employee IDuserID,但在查询中有EmployeeIDeID,但没有提到userIDGROUP_CONCAT中缺少Date值的引号。
无论如何,当前查询都是非法的,因为集合函数之外的表达式在功能上不依赖于GROUP BY子句。
这将构建、准备和执行您要使用的查询:

SELECT
    GROUP_CONCAT(
        'TIMESTAMPDIFF(
            HOUR,
            SUM(CASE WHEN `Remarks` = ''IN''  AND `Date` = ''', `Date`, ''' THEN `Time` END),
            SUM(CASE WHEN `Remarks` = ''OUT'' AND `Date` = ''', `Date`, ''' THEN `Time` END)
        ) AS ''', `Date`, ''''
    ) INTO @sql
FROM (SELECT DISTINCT `Date` FROM `tblattendance`) t;

SET @sql = CONCAT('SELECT e.Fullname, e.EmployeeID, ', @sql, ' FROM tblemployee e LEFT JOIN tblattendance a ON a.userID = e.userID GROUP BY e.userID');

SELECT @sql;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

这是一个db<>fiddle
请注意,当您缺少INOUT条目时,或者当IN在某一天而OUT在第二天时,这种方法将创建垃圾输出。

相关问题