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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:53:09+00:00 2026-06-05T14:53:09+00:00

For the question, Let’s say I have a table that holds the following data

  • 0

For the question, Let’s say I have a table that holds the following data :

1) Name
2) Mood
3) DateTime

I could insert records like :

Andy    Happy    '11.06.2012 - 14.06.07' -- Inserted on 11.06.2012 @ 19:12.32
Arthur  Angry    '11.06.2012 - 15.06.57' -- Inserted on 11.06.2012 @ 17:12.32
Andy    Sad      '11.06.2012 - 14.34.05' -- Inserted on 11.06.2012 @ 17:12.32
Arthur  Happy    '11.06.2012 - 13.34.05' -- Inserted on 11.06.2012 @ 14:12.32

I would like to get the “duration” information related to these moods changes !

My table holds thousands of hundred or records and I cannot afford having a process that takes too much time. What would be the best way of calculating this ?

  • Trigger “After insert”, filling a “Duration” column ?
  • Stored procedure that fill a previously created “Duration” column ?
  • Calculated column ?
  • A view (I already tried that and it takes more than 2 seconds to display, which is totally unacceptable)
  • Another idea ?

Thanks for your help !

Important edit :
The mood records arrive grouped into packet and we cannot be sure that already inserted records have smaller dates! (see the above comments next to my records)

  • 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-05T14:53:11+00:00Added an answer on June 5, 2026 at 2:53 pm

    A possible SQL version:

    WITH CTE AS(
        SELECT ROW_NUMBER()OVER(PARTITION BY [Name] ORDER BY [Time])As RowNum
        , *
        FROM @table T
    )
    SELECT 
        DiffSec=DATEDIFF(s,[Time],(SELECT [Time] FROM CTE c2 WHERE c2.[Name]=CTE.[Name] AND c2.RowNum=CTE.RowNum+1))
        , [Name]
        , Mood
        , [Time]
    FROM CTE 
    ORDER BY [Name],[RowNum]
    

    Result:

    DiffSec Name    Mood    Time
    1678    Andy    Happy   2012-06-11 14:06:07.000
    NULL    Andy    Sad     2012-06-11 14:34:05.000
    5228    Arthur  Angry   2012-06-11 14:06:57.000
    NULL    Arthur  Happy   2012-06-11 15:34:05.000
    

    Your test data:

    declare @table table(name varchar(10),mood varchar(10),time datetime);
    insert into @table values('Andy','Happy',convert(datetime,'11.06.2012 14:06:07',104));
    insert into @table values('Arthur','Angry',convert(datetime,'11.06.2012 14:06:57',104));
    insert into @table values('Andy','Sad',convert(datetime,'11.06.2012 14:34:05',104));
    insert into @table values('Arthur','Happy',convert(datetime,'11.06.2012 15:34:05',104));
    

    Edit Self-Joining a CTE seems to be a very bad idea (“If you self join the CTE it will kill you“). I’ve tested my query with 500000 records in a temporary table and cancelled the query after 30 minutes.

    Here’s a much faster approach(4 seconds for all) using a sub-query (with your commented schema):

    SELECT T.*
        ,(SELECT DATEDIFF(s,MAX(T2.Time),T.Time)
          FROM dbo.Temp T2
          WHERE T2.HE_Id = T.HE_Id
              AND T2.Time < T.Time
        ) AS DiffSec
    FROM dbo.Temp AS T
    ORDER BY HE_Id ,DiffSec
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

General question: Let's say you have a list of vertices that you pass into
Maybe a basic question but let us say I have a string that is
Here's a question: let's say we have a png image 200x200 that is transparent
I have a though question :) Let's say you get some data which is
A dumb question: Let's say I have an object that i store in viewstate.
I have the following question: Let's assume the code below: ... <h:form id=..> <table>
Beginner rails/javascript question: Let's say that I have a simple Circle model in a
I have a simple question: Let's supose i have the next span: <span class=data
Possible stupid question: let's say I have two apps contained within one project. Can
This is a best practices question Let say, I have a class object, like

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.