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 7877123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:17:01+00:00 2026-06-03T03:17:01+00:00

I am creating a clock-in / clock-out system for employees. There is a tbl_clockins

  • 0

I am creating a clock-in / clock-out system for employees.

There is a tbl_clockins which contains records of each clock-in/clock-out session with information on whether each session is paid, how late the employee was for that session or how much overtime they did, etc.

There is another table called tbl_user_work_settings where the manager can set which days employees are on holiday, or have taken off on sickness etc.

I am creating some reports where I need totals for each employee, e.g. total days taken as holiday by each employee wihin a given date range. I have a very long query which actually gets all the required information, but it is huge and somewhat inefficient. Is there any way to make it smaller/more efficient? Any help is appreciated.

// get total days worked, unpaid days, bank holidays, holidays, sicknesses
// and absences within given date range for given users            
$sql = "SELECT us.username, daysWorked, secondsWorked,
            unpaidDays, bankHolidays, holidays, sicknesses, absences
FROM
  (SELECT username FROM users WHERE clockin_valid='1') us
  LEFT JOIN (
    SELECT   username, selectedDate, count(isUnpaid) AS unpaidDays
    FROM     tbl_user_work_settings
    WHERE    isUnpaid = '1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) u ON us.username=u.username
  LEFT JOIN (
    SELECT   username, count(isBankHoliday) AS bankHolidays
    FROM     tbl_user_work_settings
    WHERE    isBankHoliday='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) bh ON us.username=bh.username
  LEFT JOIN (
    SELECT   username, count(isHoliday) AS holidays
    FROM     tbl_user_work_settings
    WHERE    isHoliday='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) h ON us.username=h.username
  LEFT JOIN (
    SELECT   username, count(isSickness) AS sicknesses
    FROM     tbl_user_work_settings
    WHERE    isSickness='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) s ON us.username=s.username
  LEFT JOIN (
    SELECT   username, count(isOtherAbsence) AS absences
    FROM     tbl_user_work_settings
    WHERE    isOtherAbsence='1'
         AND selectedDate>='$startDate'
         AND selectedDate<='$endDate'
    GROUP BY username
  ) a ON us.username=a.username
  LEFT JOIN (
    SELECT   username, count(DISTINCT DATE(in_time)) AS daysWorked,
                SUM(seconds_duration) AS secondsWorked
    FROM     tbl_clockins
    WHERE    DATE(in_time)>='$startDate'
         AND DATE(in_time)<='$endDate'
    GROUP BY username
  ) dw ON us.username=dw.username";

if(count($selectedUsers)>0)
  $sql .= " WHERE (us.username='"
       .  implode("' OR us.username='", $selectedUsers)."')";

$sql .= " ORDER BY us.username ASC";
  • 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-06-03T03:17:02+00:00Added an answer on June 3, 2026 at 3:17 am

    You can use SUM(condition) on a single use of the tbl_user_work_settings table:

    // get total days worked, unpaid days, bank holidays, holidays, sicknesses
    // and absences within given date range for given users            
    $sql = "
      SELECT      users.username,
                  SUM(ws.isUnpaid      ='1')       AS unpaidDays,
                  SUM(ws.isBankHoliday ='1')       AS bankHolidays,
                  SUM(ws.isHoliday     ='1')       AS holidays,
                  SUM(ws.isSickness    ='1')       AS sicknesses,
                  SUM(ws.isOtherAbsence='1')       AS absences,
                  COUNT(DISTINCT DATE(cl.in_time)) AS daysWorked,
                  SUM(cl.seconds_duration)         AS secondsWorked
      FROM        users
        LEFT JOIN tbl_user_work_settings           AS ws
               ON ws.username = users.username
              AND ws.selectedDate  BETWEEN '$startDate' AND '$endDate'
        LEFT JOIN tbl_clockins                     AS cl
               ON cl.username = users.username
              AND DATE(cl.in_time) BETWEEN '$startDate' AND '$endDate'
      WHERE       users.clockin_valid='1'";
    
    if(count($selectedUsers)>0) $sql .= "
              AND users.username IN ('" . implode("','", $selectedUsers) . "')";
    
    $sql .= "
      GROUP BY    users.username
      ORDER BY    users.username ASC";
    

    By the way (and perhaps more for the benefit of other readers), I really hope that you are avoiding SQL injection attacks by properly escaping your PHP variables before inserting them into your SQL. Ideally, you shouldn’t do that at all, but instead pass such variables to MySQL as the parameters of a prepared statement (which don’t get evaluated for SQL): read more about Bobby Tables.

    Also, as an aside, why are you handling integer types as strings (by enclosing them in single quote characters)? That’s needless and a waste of resource in MySQL having to perform unnecessary type conversion. Indeed, if the various isUnpaid etc. columns are all 0/1, you can change the above to remove the equality test and just use SUM(ws.isUnpaid) etc. directly.

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

Sidebar

Related Questions

Okay, I'm creating a challenge system on a website. I will list out in
I am creating a form, which will send out the details via email upon
am creating one application for student. I need to show digital clock with remaining
friends, I am new to Iphone development, I am creating a simple analog clock,
I'm creating one-click python installer (integrated with my application). Is there any way to
I was trying out the method of creating a background music for a java
I'm in startup of designing a client/server audio system which can stream audio arbitrarily
I created a Tree View out of a file system in ASP.NET. When the
I'm creating a WPF app and have a system tray icon with a context
I'm creating a System-Tray only application. It's somewhat complicated to have the icon without

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.