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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:31:15+00:00 2026-06-18T02:31:15+00:00

I hope this can clearly explain what I am looking for. I have searched

  • 0

I hope this can clearly explain what I am looking for. I have searched read through a few articles on this site, but haven’t found what I am looking for. I have also spent close to 3 hours trying to figure this out on my own.

I am trying to count the number of records and SUM the WorkTime. Here is my query I have been working with.

SELECT Log.User
 , sum(if(hour(endtime) = 0, 1, 0)) AS Midnight
 , sum(if(hour(endtime) = 1, 1, 0)) AS `1AM`
 , sum(if(hour(endtime) = 2, 1, 0)) AS `2AM`
 , sum(if(hour(endtime) = 3, 1, 0)) AS `3AM`
 , sum(if(hour(endtime) = 4, 1, 0)) AS `4AM`
 , sum(if(hour(endtime) = 5, 1, 0)) AS `5AM`
 , sum(if(hour(endtime) = 6, 1, 0)) AS `6AM`
 , sum(if(hour(endtime) = 7, 1, 0)) AS `7AM`
 , sum(if(hour(endtime) = 8, 1, 0)) AS `8AM`
 , sum(if(hour(endtime) = 9, 1, 0)) AS `9AM`
 , sum(if(hour(endtime) = 10, 1, 0)) AS `10AM`
 , sum(if(hour(endtime) = 11, 1, 0)) AS `11AM`
 , sum(if(hour(endtime) = 12, 1, 0)) AS `12PM`
 , sum(if(hour(endtime) = 13, 1, 0)) AS `1PM`
 , sum(if(hour(endtime) = 14, 1, 0)) AS `2PM`
 , sum(if(hour(endtime) = 15, 1, 0)) AS `3PM`
 , sum(if(hour(endtime) = 16, 1, 0)) AS `4PM`
 , sum(if(hour(endtime) = 17, 1, 0)) AS `5PM`
 , sum(if(hour(endtime) = 18, 1, 0)) AS `6PM`
 , sum(if(hour(endtime) = 19, 1, 0)) AS `7PM`
 , sum(if(hour(endtime) = 20, 1, 0)) AS `8PM`
 , if(hour(endtime) = 20, sec_to_time(sum(time_to_sec(endtime) - time_to_sec(starttime))), 0) AS `8PM Time`
 , sum(if(hour(endtime) = 21, 1, 0)) AS `9PM`
 , sum(if(hour(endtime) = 22, 1, 0)) AS `10PM`
 , sum(if(hour(endtime) = 23, 1, 0)) AS `11PM`
FROM
  (
  SELECT user
   , controlnumber
   , starttime
   , endtime
   , timediff(endtime, starttime) AS Worktime
FROM
   atrtaxcert.ordertimeworked
  ) AS Log
GROUP BY
   Log.User;

These start and end times are only minutes apart.
Any guidance is much appreciated. This is my first post here, and was not able to provide any images to help describe.

  • 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-18T02:31:17+00:00Added an answer on June 18, 2026 at 2:31 am

    If starttime and endtime are TIME datatypes, then use the TIME_TO_SEC function and do a subtraction. Total up the seconds, and then convert the total to a string representation.

    SELECT `Log`.`User`
         , ...
         , SUM(HOUR(`Log`.endtime)=20) AS `8PM_count` 
         , SUM(IF(HOUR(`Log`.endtime)=20,work_seconds,0) AS `8PM_seconds`
         , SEC_TO_TIME(SUM(IF(HOUR(`Log`.endtime)=20,`Log`.work_seconds,0) AS `8PM_hhhmmss`
         , ...
      FROM ( SELECT
                  , TIME_TO_SEC(endtime)-TIME_TO_SEC(starttime) AS work_seconds
           ) `Log`
     GROUP
        BY `Log`.`User`
    

    NOTE: this:

    SELECT HOUR(endtime)=0 AS foo
    

    is shorthand equivalent to

    SELECT IF(HOUR(endtime) = 0, 1, 0) AS foo
    

    If starttime and endtime are DATETIME values, the you can use the TIMESTAMPDIFF function to calculate the difference in seconds:

    SELECT `Log`.`User`
         , ...
         , SUM(HOUR(`Log`.endtime)=20) AS `8PM_count` 
         , SUM(IF(HOUR(endtime)=20,TIMESTAMPDIFF(SECOND,`Log`.starttime,`Log`.endtime),0) AS `8PM_seconds`
         , ...
      FROM (
           ) `Log`
     GROUP
        BY `Log`.`User`
    

    (You probably want to ignore the values returned when e.g. starttime = ’23:59:00′ and endtime = ’00:01:00′, and that would require another conditional test.)

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

Sidebar

Related Questions

I hope i can explain this clearly. I have a NSMutableArray * myMut. I
I hope I can explain this clearly. I have a table, lets call it
I hope I can explain this clearly enough. I have my main form (A)
I hope I can explain this clearly: I have two tables with the following
I hope I can explain this right I have two input fields that require
Newbie technical difficulty here. Hope I can explain this clearly. in My DB, I
I hope I can ask this question clearly, I have a viewController that I
I hope I can explain this clearly.. Is there a way to configure the
This is a somewhat long question, but I hope I can express it clearly.
This is a very strange problem, and I just hope that I can clearly

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.