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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:38:25+00:00 2026-06-13T01:38:25+00:00

Calculating time frames between status using SQL 2008/2012 I’ve the following table who store

  • 0

Calculating time frames between status using SQL 2008/2012

I’ve the following table who store the status of a student

+----+-----------+------------------+---------+---------+
| ID | PERSON_ID |    TIMESTAMP     | IN_HOME | STUDYNG |
+----+-----------+------------------+---------+---------+
|  1 |         1 | 17/10/2012 19:00 |       0 |       0 |
|  2 |         1 | 17/10/2012 19:02 |       1 |       0 |
|  3 |         1 | 17/10/2012 19:03 |       1 |       1 |
|  4 |         1 | 17/10/2012 19:04 |       1 |       1 |
|  5 |         1 | 17/10/2012 19:05 |       1 |       0 |
|  6 |         1 | 17/10/2012 19:10 |       0 |       0 |
|  7 |         1 | 17/10/2012 19:12 |       0 |       0 |
|  8 |         1 | 17/10/2012 19:20 |       1 |       0 |
|  9 |         1 | 17/10/2012 19:25 |       1 |       0 |
| 10 |         1 | 17/10/2012 19:26 |       1 |       1 |
| 11 |         1 | 17/10/2012 19:30 |       1 |       0 |
+----+-----------+------------------+---------+---------+

And i would like to produce results in 2 ways to make some reports:

I:

+-----------+------------------+------------------+---------+---------+
| PERSON_ID |      START       |       END        | IN_HOME | STUDYNG |
+-----------+------------------+------------------+---------+---------+
|         1 | 17/10/2012 19:00 | 17/10/2012 19:02 |       0 |       0 |
|         1 | 17/10/2012 19:02 | 17/10/2012 19:03 |       1 |       0 |
|         1 | 17/10/2012 19:03 | 17/10/2012 19:05 |       1 |       1 |
|         1 | 17/10/2012 19:05 | 17/10/2012 19:10 |       1 |       0 |
|         1 | 17/10/2012 19:10 | 17/10/2012 19:20 |       0 |       0 |
|         1 | 17/10/2012 19:20 | 17/10/2012 19:26 |       1 |       0 |
|         1 | 17/10/2012 19:26 | 17/10/2012 19:30 |       1 |       1 |
+-----------+------------------+------------------+---------+---------+

II:

+-----+------------------+------------------+--------+---------+----------+----------+
| PID |      START       |       END        | InHOME | TotTIME | FreeTIME | StudTIME |
+-----+------------------+------------------+--------+---------+----------+----------+
|   1 | 17/10/2012 19:00 | 17/10/2012 19:02 |      0 | 2min    | 2min     | 0min     |
|   1 | 17/10/2012 19:02 | 17/10/2012 19:10 |      1 | 8min    | 6min     | 2min     |
|   1 | 17/10/2012 19:10 | 17/10/2012 19:20 |      0 | 10min   | 10min    | 0min     |
|   1 | 17/10/2012 19:20 | 17/10/2012 19:26 |      1 | 6min    | 6min     | 0min     |
+-----+------------------+------------------+--------+---------+----------+----------+

What’s the best solution to solve this problems?

  • 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-13T01:38:26+00:00Added an answer on June 13, 2026 at 1:38 am

    First one may look like this. I just don’t understand, why you have STUDYING = 0 in last row of report I (mistake may be?)

    select
        T.PERSON_ID,
        min(T.[TIMESTAMP]) as START,
        CALC.[TIMESTAMP] as [END],
        T.IN_HOME, T.STUDYNG
    from @Temp as T
        cross apply
        (
            select top 1 TT.*
            from @Temp as TT
            where 
                TT.PERSON_ID = T.PERSON_ID and TT.[TIMESTAMP] > T.[TIMESTAMP] and
                (TT.IN_HOME <> T.IN_HOME or TT.STUDYNG <> T.STUDYNG)
            order by TT.[TIMESTAMP] asc
        ) as CALC
    group by 
        T.PERSON_ID,
        CALC.[TIMESTAMP],
        T.IN_HOME, T.STUDYNG
    order by START
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am calculating current time Interval using following codes: NSTimeInterval tInt=[[NSDate date] timeIntervalSince1970]; int
I am having a problem with calculating time difference between two timeZones. If I
I'm having trouble calculating a time (hours:minutes) difference between three time values presented as
I have a few methods on my entity class (calculating time intervals between creation
I am having trouble calculating time difference. I am getting one time from web
I've tried all the time calculating examples I found on this site but somehow
i like to set timer for calculating execution time in c# for particular process
I've created a custom report in Fetch XML with an expression calculating the time
I am calculating something that takes some time. For that I've created an inner
Possible Duplicate: Calculating the Difference Between Two Java Date Instances How do I get

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.