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

  • Home
  • SEARCH
  • 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 79473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:09:05+00:00 2026-05-10T21:09:05+00:00

I’m writing a simple time tracking program to manage my own projects. I’m a

  • 0

I’m writing a simple time tracking program to manage my own projects. I’m a big fan of keeping reporting code in the database, so I’ve been attempting to create a few sprocs that generate the invoices and timesheets etc.

I have a table that contains Clock Actions, IE ‘Punch In’, and ‘Punch Out’. It also contains the user that did this action, the project associated with the action, and the current date/time.

I can select from this table to get clock in’s for a specific time/project/and user, but I want to aggregate it down so that each clock in and out is converted from 2 rows to a single row containing total time.

For example, here is a sample output:

ClockActionID        ActionType DateTime -------------------- ---------- ----------------------- 17                   1          2008-11-08 18:33:56.000 18                   2          2008-11-08 18:33:59.587 19                   1          2008-11-08 18:34:01.023 20                   2          2008-11-08 18:34:02.037 21                   1          2008-11-08 18:45:06.317 22                   2          2008-11-08 18:46:14.597 23                   1          2008-11-08 18:46:16.283 24                   2          2008-11-08 18:46:17.173 25                   1          2008-11-08 18:50:37.830 26                   2          2008-11-08 18:50:39.737 27                   1          2008-11-08 18:50:40.547  (11 row(s) affected) 

Where ActionType 1 is ‘ClockIn’ and ActionType 2 is ‘ClockOut’. I also pruned out the User, Project, and Description columns for brevity.

I need to generate, in pure SQL, a result set like:

Description   |    Total Time 

For each ClockIn / ClockOut Pair.

I figure this will actually be fairly simple, I’m just not quite sure which way to approach it.

EDIT: The user will be able to clock into multiple projects simultaneously, though by first narrowing down the result set to a single project, this shouldn’t make any difference to the logic here.

  • 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. 2026-05-10T21:09:05+00:00Added an answer on May 10, 2026 at 9:09 pm

    I agree that the design isn’t the greatest–an event based structure with a single row for start-end will probably save you a lot of time. In that case, you could create a record with a null end-date when someone clocks in. Then, fill in the end date when they clock out.

    But, that’s not what you asked. This is the solution to your problem:

    DECLARE @clock TABLE (ClockActionID INT PRIMARY KEY IDENTITY, ActionType INT, ActionDateTime DATETIME)  INSERT INTO @clock (ActionType, ActionDateTime) VALUES (1,'20080101 00:00:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (2,'20080101 00:01:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (1,'20080101 00:02:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (2,'20080101 00:03:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (1,'20080101 00:04:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (2,'20080101 00:05:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (1,'20080101 00:06:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (2,'20080101 00:07:00') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (1,'20080101 00:08:12') INSERT INTO @clock (ActionType, ActionDateTime) VALUES (2,'20080101 00:09:00')  -- Get the range SELECT ActionDateTime CheckIn,    (SELECT TOP 1 ActionDateTime     FROM @clock C2     WHERE C2.ActionDateTime > C.ActionDateTime) CheckOut    FROM @clock C WHERE ActionType = 1  -- Get the duration SELECT DATEDIFF(second, ActionDateTime,    (SELECT TOP 1 ActionDateTime     FROM @clock C2     WHERE C2.ActionDateTime > C.ActionDateTime)   ) / 60.0 Duration_Minutes FROM @clock C WHERE ActionType = 1 

    Note that I’m using a table variable which works with MS SQL Server just for testing. Change as needed. Also note that SQL Server 2000 does not perform well with queries like this. Here are the test results:

    CheckIn                 CheckOut 2008-01-01 00:00:00.000 2008-01-01 00:01:00.000 2008-01-01 00:02:00.000 2008-01-01 00:03:00.000 2008-01-01 00:04:00.000 2008-01-01 00:05:00.000 2008-01-01 00:06:00.000 2008-01-01 00:07:00.000 2008-01-01 00:08:12.000 2008-01-01 00:09:00.000  Duration_Minutes 1.000000 1.000000 1.000000 1.000000 0.800000 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Regarding mutability: F# allows you to be pragmatic. I rarely… May 11, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer Silent install was removed due to misuse. You basically have… May 11, 2026 at 8:27 pm
  • Editorial Team
    Editorial Team added an answer Use Url.Action and Url.RouteUrl methods. May 11, 2026 at 8:27 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.