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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:30:29+00:00 2026-05-29T09:30:29+00:00

For the following table : Id Timestamp AssignedTo 1 2012-01-01 User1 2 2012-01-02 User2

  • 0

For the following table :

Id    Timestamp    AssignedTo
1     2012-01-01   User1
2     2012-01-02   User2
3     2012-01-10   User3
4     2012-01-15   User1

what would be the general approach in MS SQL Server to calculating how many days/hours/minutes the entity was assigned to a specific user?

As a developer (with basic SQL knowledge), I’ve always opted for Functions or Stored procedures when doing these kind of queries (these kind = queries including results based on the difference between two rows in a set).

In this example, I’d have a cursor iterating over the items, keeping the previous one in a variable, and then calculating the difference. However, I’ve been told that this is a serious performance concern, especially on large sets.

EDIT: Sample results

User    TotalTime
User1   23d
User2   8d
User3   5d

i.e. the total time that an item was assigned to a specific user. User1’s TotalTime is 23 days because it has been assigned to him since 2012-01-15 (and it’s 2012-02-06 now), along with the first day it was assigned to him.

  • 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-29T09:30:30+00:00Added an answer on May 29, 2026 at 9:30 am

    In this example I’d use a subquery within the select statement to get the end date for each row, then use this end date to get the time assigned to a specific user. The below is SQL Server syntax but the same principal can be applied whatever the RDBMS.

    SELECT  AssignedTo, 
            SUM(DATEDIFF(DAY, TimeStamp, ISNULL(EndDate, GETDATE()))) [Days]
    FROM    (   SELECT  ID,
                        [TimeStamp],
                        AssignedTo,
                        (   SELECT  MIN(TimeStamp)
                            FROM    [YOURTABLE] b
                            WHERE   b.TimeStamp > a.TimeStamp
                        ) [EndDate]
                FROM    [YOURTABLE] a
            )  a
    GROUP BY AssignedTo
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table in my postgreSQL 8.3.14 database timestamp status 2012-03-12 19:15:01
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
Consider the following SQL: CREATE TABLE USER1 ( pkUSER1_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
Consider the following table tweets tweet_id call_id id_str timestamp text -------------------------------------------------- 1 11 12345
Say I have a 'user_log' table with the following field: id user_id status_text timestamp
my MySQL Server (version: 5.0.51a-3ubuntu5.8) I have the following InnoDB table: CREATE TABLE `kontostaende`
Please help me to write the following sql. I have a table like this,
I have the following query: SELECT timestamp, COUNT(*) FROM table GROUP BY timestamp But
I have the following table CREATE TABLE [dbo].[LogFiles_Warehouse]( [id] [int] IDENTITY(1,1) NOT NULL, [timestamp]
I have a table with a timestamp field including these values: 2012-03-12 12:53:34 2012-03-12

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.