Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6990337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:20:06+00:00 2026-05-27T19:20:06+00:00

I am creating a timeclock system and management website. I am trying to pull

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:20:07+00:00Added an answer on May 27, 2026 at 7:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Creating a patch utility that will update my current website with my patch. when
Creating a server-side socket will fail if I'm trying to use the same port
Creating simple php login scripts is easy, with simple one table mysql integration. I
Creating a login system for something and am getting: Warning: mysql_result(): supplied argument is
Creating an Iphone application, I used to perform INSERT QUERY on different databases. But
Creating an index MySQL After I have added the FULLTEXT index, how do I
Creating Your Own Abstract Processor is a quick paper on what I am trying
creating users through mysql admin, but unable to login mysql command line following on-line
Creating a website using simple html(not html5) and css but got stock in mouseover
Creating a voting system just like stackoverflow. Questions like this have been asking several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.