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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:06:01+00:00 2026-06-08T18:06:01+00:00

I´m using SQL Server 2008, how can I calculate the time between On and

  • 0

I´m using SQL Server 2008, how can I calculate the time between On and Off status? I have the following table (ordered by timestamp):

ID | EQUIP_ID | TIMESTAMP           | STATUS (1 on/0 off)
1  |     1    | 21/05/2012 13:00:00 |   1
3  |     1    | 21/05/2012 13:04:00 |   1
4  |     1    | 21/05/2012 13:05:00 |   0
6  |     1    | 21/05/2012 13:09:00 |   1
7  |     1    | 21/05/2012 13:10:00 |   1
9  |     1    | 21/05/2012 13:12:00 |   1
10 |     1    | 21/05/2012 13:13:00 |   0
10 |     1    | 21/05/2012 13:14:00 |   1
10 |     1    | 21/05/2012 13:15:00 |   0

And I expect a result like this:

EQUIP_ID |    START             |          END           | STATUS
    1    | 21/05/2012 13:00:00  |   21/05/2012 13:05:00  |   1       (WORKING)
    1    | 21/05/2012 13:05:00  |   21/05/2012 13:09:00  |   0       (STOPPED)
    1    | 21/05/2012 13:09:00  |   21/05/2012 13:13:00  |   1
    1    | 21/05/2012 13:13:00  |   21/05/2012 13:14:00  |   0
    1    | 21/05/2012 13:14:00  |   21/05/2012 13:15:00  |   1

I’ve tried some functions for gaps and islands but didn’t work and I don’t know what I’m missing…

  • 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-08T18:06:02+00:00Added an answer on June 8, 2026 at 6:06 pm

    Here’s my take on it. Assuming your table is called “MyData”:

    WITH operating AS
    (
        SELECT
            d.EQUIP_ID
            , d.[TIMESTAMP]
            , d.[STATUS]
            , ROW_NUMBER() OVER (PARTITION BY EQUIP_ID ORDER BY [TIMESTAMP]) RowNum
            , ROW_NUMBER() OVER (PARTITION BY EQUIP_ID ORDER BY [TIMESTAMP]) -
                ROW_NUMBER() OVER (PARTITION BY EQUIP_ID, [STATUS] ORDER BY [TIMESTAMP]) AS [Group]
        FROM 
            MyData d
    )
    SELECT 
        state1.EQUIP_ID
        , MIN(state1.[TIMESTAMP]) [START]
        , MAX(state2.[TIMESTAMP]) [END]
        , state1.STATUS 
    FROM 
        operating state1
    LEFT JOIN
        operating state2 
        ON 
        state1.RowNum = state2.RowNum - 1
    WHERE
        state2.[TIMESTAMP] IS NOT NULL
    GROUP BY  
        state1.EQUIP_ID, state1.[STATUS], state1.[Group]
    ORDER BY 
        MIN(state1.[TIMESTAMP])
    

    It makes use of the ROW_NUMBER() function to determine changes in statuses for each EQUIP_ID. Then it simply finds when a status started (MIN([TIMESTAMP])), then I match it up with the time it ended (MAX([TIMESTAMP])) in the next row (see the self-join on RowNum). The WHERE eliminates the last row which has no end time. The results I get are:

    EQUIP_ID | START                   | END                     | STATUS 
    ---------+-------------------------+-------------------------+-------
           1 | 2012-05-21 13:00:00.000 | 2012-05-21 13:05:00.000 |      1
           1 | 2012-05-21 13:05:00.000 | 2012-05-21 13:09:00.000 |      0
           1 | 2012-05-21 13:09:00.000 | 2012-05-21 13:13:00.000 |      1
           1 | 2012-05-21 13:13:00.000 | 2012-05-21 13:14:00.000 |      0
           1 | 2012-05-21 13:14:00.000 | 2012-05-21 13:15:00.000 |      1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL Server 2008 . I have a table with the following
I'm using SQL Server 2008. I have a table with x amount of rows.
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)
Using SQL Server 2008. I have a table variable with a single column and
We are using SQL Server 2008 Enterprise version. We have a large table FooTable
I'm using SQL Server 2008 . How can I pass Table Valued parameter to
I am using SQL Server 2008. I have a table (TBL_FILE) that stores user
I'm using SQL Server 2008 R2 and have a simple table with a column
I am using SQL Server 2008. I have tried to execute the following: BEGIN
I’m using SQL Server 2008. I have data as in this table: Team Email

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.