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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:47:30+00:00 2026-05-20T07:47:30+00:00

I am trying to build a query that analyzes data in our time tracking

  • 0

I am trying to build a query that analyzes data in our time tracking system. Every time a user punches in or out, it makes a row recording the punch time. So if you punch in at 9:00 and punch out at 5:00 there are two rows with those date stamps recorded accordingly. I need a query that will iterate over the rows at basically sum the datediff between workingpunch_ts (the timestamp column) in hours.

Each row does have an identifier that signifies if the punch is a punch in, or punch out (inout_id, 1 for in, 2 for out).

So for example if you had

ID  | workingpunch_ts         | inout_id
----------------------------------------------
123 | 2011-02-16 09:00:00.000 | 1
124 | 2011-02-16 17:00:00.000 | 2

That would yield a 8 hours. Now I just need to repeat that process for every pair of rows in the table.

Thoughts on how to accomplish this?

  • 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-20T07:47:30+00:00Added an answer on May 20, 2026 at 7:47 am

    In hours, sure

    select empid, cast(datediff(d,0,workingpunch_ts) as datetime),
        SUM(case when inout_id = 2 then 1 else -1 end *
        datediff(MI, datediff(d,0,workingpunch_ts), workingpunch_ts))/60.0 as Hours
    from clock
    where workingpunch_ts between '20110201' and '20110228 23:59:59.999'
    group by empid, datediff(d,0,workingpunch_ts)
    

    As long as the in and outs are paired, you add all the outs and remove all the ins, e.g.

     - IN  (9)
     + OUT (12)
     - IN  (13:15)
     + OUT (17)
    

    The main code is in the 2nd and 3rd lines
    The datediff-datediff works out the minutes from midnight for each workingpunch_ts, and if it is a punchout, it is made negative using the CASE inout_id statement.

    The others are added for real life scenarios where you need to group by employee and day, within a date range.

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

Sidebar

Related Questions

I'm trying to build a query that will order results by date and time,
I am trying to build a query that will select based on a DateTime
Im trying to build a query that will fetch all changed rows from a
I'm trying to build a PostGIS query that uses an alias of the JOIN
I'm trying to build a MySQL query that uses the rows in a lookup
I'm trying to build a query for a medical database that counts the number
I'm trying to build a query in Postgresql that will be used for a
I'm trying to build a jpql query that gives me the average value in
I'm trying to build a query that will pull events from a DB based
I am trying to build a sql query that I think it involves inner

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.