计算总加班时间 MySQL
我正在创建一个时钟系统和管理网站。我正在尝试使用 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_id
s and the total hours they worked. To create a table of employee_id
s 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_id
s 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个怎么样?
当您使用 SQL 实现复杂的业务逻辑时,嵌套查询具有一个关键优势:它们使您可以测试逻辑的每个级别并以可靠的方式构建它。
How about this?
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.
只需将其添加到 Ollie 的上述答案中即可:
您需要命名派生表以使代码正常工作。
Just add this to the above answer by Ollie:
You need to name your derived table for the code to work.