计算总加班时间 MySQL

发布于 2024-12-22 03:43:40 字数 1284 浏览 6 评论 0原文

我正在创建一个时钟系统和管理网站。我正在尝试使用 MySQL 查询来获取特定部门的加班时间。

时钟更新名为 events 的 MySQL 表,该表存储员工 ID、他们正在从事的工作、time_in 和 time_out 。我可以生成一个包含每个 employee_id 的总工作时间的表格:

SELECT employee_id, 
       SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))) AS hours 
  FROM events 
 GROUP BY employee_id;

这将返回一个包含 employee_id 及其工作总时间的表格。为了创建一个employee_id加班表,我使用:

SELECT employee_id,
       IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
          HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
          0) AS overtime 
  FROM events 
  GROUP BY employee_id;

这将返回一个employee_id表以及他们加班的时间。

当我想找到所有员工的加班总数时,我遇到了麻烦。我以为我可以将 IF 函数放在 SUM() 内查找超时,但是当我运行以下语句时:

SELECT SUM(IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
              HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
              0)) AS overtime 
 FROM events;

我收到此错误:

ERROR 1111 (HY000): Invalid use of group function

我有对此进行了全面查找,但几乎没有发现 SUM(IF(SUM())) 函数。

注意:加班费必须单独计算,我不能只采用总工作时间/员工人数 - 40。如果一名工人每周工作 30 小时,另一名工人每周工作 50 小时,则有 10 小时的加班时间,但平均而言没有加班时间。

I am creating a timeclock system and management website. I am trying to pull the amount of overtime for a particular department with a MySQL query.

The timeclocks update a MySQL table called events, which stores the employee_id, the job they are working on, the time_in and the time_out. I can produce a table of the total hours worked from each employee_id with:

SELECT employee_id, 
       SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))) AS hours 
  FROM events 
 GROUP BY employee_id;

This will return a table of employee_ids and the total hours they worked. To create a table of employee_ids overtime I use:

SELECT employee_id,
       IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
          HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
          0) AS overtime 
  FROM events 
  GROUP BY employee_id;

This returns a table of employee_ids and the amount of overtime they have worked.

The trouble I am having starts when I want to find a total of the overtime worked for all employees. I assumed I would be able to just put the IF function to find overtime inside of SUM() however when i run the following statement:

SELECT SUM(IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
              HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
              0)) AS overtime 
 FROM events;

I get this error back:

ERROR 1111 (HY000): Invalid use of group function

I have looked all over for this but have found little in way of SUM(IF(SUM())) functions.

Note: The overtime has to be calculated individually, I cant just take total hours worked / number of employees - 40. If one worker works 30 hours in a week, and another works 50, there are 10 hours of overtime, but the average says there isn't any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱给你人给你 2024-12-29 03:43:40

这个怎么样?

SELECT SUM(overtime)
  FROM
  (
    SELECT employee_id,
           IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
              HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
              0) AS overtime 
      FROM events 
     GROUP BY employee_id
  )TOTAL

当您使用 SQL 实现复杂的业务逻辑时,嵌套查询具有一个关键优势:它们使您可以测试逻辑的每个级别并以可靠的方式构建它。

How about this?

SELECT SUM(overtime)
  FROM
  (
    SELECT employee_id,
           IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
              HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
              0) AS overtime 
      FROM events 
     GROUP BY employee_id
  )TOTAL

When you're using SQL to implement complex business logic, nested queries have a key advantage: they let you test each level of the logic and build it up in a reliable way.

究竟谁懂我的在乎 2024-12-29 03:43:40

只需将其添加到 Ollie 的上述答案中即可:

SELECT SUM(overtime)
  FROM
  (
    SELECT employee_id,
           IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
              HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
              0) AS overtime 
      FROM events
     GROUP BY employee_id
  ) TOTAL_OVERTIME

您需要命名派生表以使代码正常工作。

Just add this to the above answer by Ollie:

SELECT SUM(overtime)
  FROM
  (
    SELECT employee_id,
           IF(HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))>40,
              HOUR(SEC_TO_TIME(SUM(TIMEDIFF(time_out,time_in))))-40,
              0) AS overtime 
      FROM events
     GROUP BY employee_id
  ) TOTAL_OVERTIME

You need to name your derived table for the code to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文