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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:43:56+00:00 2026-06-15T10:43:56+00:00

I have this table in SQL Server: UserId Date Start Time End Time ————————————————————————————

  • 0

I have this table in SQL Server:

UserId    Date                       Start Time              End Time
------------------------------------------------------------------------------------
20       2012-04-02 00:00:00.000     NULL                    2012-04-02 09:17:57.000
20       2012-04-02 00:00:00.000    2012-04-02 09:17:57.000  2012-04-02 09:57:55.000
20       2012-04-02 00:00:00.000    2012-04-02 09:57:55.000  2012-04-02 10:04:58.000
20       2012-04-02 00:00:00.000    2012-04-02 10:04:58.000  2012-04-02 10:21:40.000
20       2012-04-02 00:00:00.000    2012-04-02 10:22:15.000  2012-04-02 10:22:20.000
20       2012-04-02 00:00:00.000    2012-04-02 10:22:56.000  2012-04-02 10:23:33.000

I want to find the difference between start time and end time and sum up the difference hours based on date

Output required is:

UserID    Time_Duration
-----------------------
 20           1:20:20      (this is example not actuals)

Can anyone help me write a SQL query..
I have tried with the below query,

select sum(sub1.TotalSeconds / 3600) as [Hours], sum((sub1.TotalSeconds % 3600) / 60)
as [Minutes], 
sum((sub1.TotalSeconds % 3600) % 60) as [Seconds],sub1.Date
from
(
SELECT
  sub.UserID,
  sub.Date,   
sum(datepart(hour, sub.end_time-sub.start_time) * 3600) + sum(datepart(minute,        sub.end_time-sub.start_time) * 60) +
sum(datepart(second, sub.end_time-sub.start_time)) as TotalSeconds
from.......
) AS sub 
 group by sub.UserID,sub.Date,sub.start_time,sub.end_time)
  as sub1 group by sub1.Date;

I get the following result

 Hours   Minutes Seconds     Date
 3      347      515    2012-04-02 00:00:00.000

But i want to add the minutes if greater then 60 it has to hours as 3+1 hr and so on. Can any 1 help me where Im making mistake

  • 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-15T10:43:57+00:00Added an answer on June 15, 2026 at 10:43 am

    Hi take a look at this:

    SQLFIDDLE

    It is NOT the most efficient and I believe it’s ugly for a code. However put it across for you to see the steps. You may get an idea out of it. Mainly using arimethic operators to get the reusults. There are extra fields as I was using it mainly to show you the data flow. I am sure other approaches are much much efficient in terms of performance. You can try.

    Query:

    select q.userid, t.date,
    q.h + round((q.m + (q.s/60))/60,0) as hh,
    round((q.m + (q.s/60)) mod 60,0) as mm,
    q.s mod 60 as ss
    from 
    (select t.userid,t.date, 
    sum(t.hours) as h,
    sum(t.minutes) as m,
    sum(t.seconds) as s
    from (select userid,date,
    TIMEDIFF(EndTime, StartTime) as duration,
    TIMESTAMPDIFF(hour,starttime,endtime) as hours,
    TIMESTAMPDIFF(minute,starttime,endtime) mod 60 as minutes,
    TIMESTAMPDIFF(second,starttime,endtime) mod 60 seconds
    from datestimes) as t
    ) as q
     ;
    

    Resutls

      USERID    DATE                            HH  MM  SS
      20        April, 02 2012 00:00:00+0000    13  4   25
    

    Updated Query only with one nested query

    Infact you may achieve this with one nested query. Apology as this whole answer is MYSQL based. So hopefully you may take the logic out to implement that using SQL Server syntax 🙂

    select t.userid, t.date, 
    (sum(t.hours) + round((sum(t.minutes) +
    sum(t.seconds)/60)/60,0)) as h,
    (round((sum(t.minutes) +
    sum(t.seconds)/60) mod 60,0)) as m,
    sum(t.seconds) mod 60 as s
    
    from 
    
    (select userid, date,
    TIMEDIFF(EndTime, StartTime) as duration,
    TIMESTAMPDIFF(hour,starttime,endtime) as hours,
    TIMESTAMPDIFF(minute,starttime,endtime) mod 60 as minutes,
    TIMESTAMPDIFF(second,starttime,endtime) mod 60 seconds
    from datestimes) as t
     ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have a table in SQL Server. This table has an image field and
I have a SQL Server 2008 DB with a table like this (Table1): ID
I have a table like this in SQL Server: varID(PK) dataID(PK) is_used A 1
I have a table like this in my database (SQL Server 2008) ID Type
I have SQL Server 2008 with a table called ProductCategories designed like this: Id
I have a table called 'Audit' in SQL Server 2005 like this: Name |
i currently have this table in SQL server: PL_ID User_ID Log_Date Out_Time In_Time Reason
I have a databas table with cca. 327 000 entries. (SQL Server 2005, Hibernate
I am writing SQL Server database, where I have a relation UserTypedText(userId, date, textId,

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.