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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:15:44+00:00 2026-06-17T05:15:44+00:00

I’ve got a table that is similar to below in MS SQL Server. id

  • 0

I’ve got a table that is similar to below in MS SQL Server.

      id | Timestamp | active
    -----+-----------+--------
      1  |    1:00   |    1
      1  |    2:00   |    1
      1  |    3:00   |    1
      1  |    4:00   |    0
      1  |    5:00   |    0
      1  |    6:00   |    1
      1  |    7:00   |    0
      1  |    8:00   |    0
      1  |    9:00   |    0
      1  |   10:00   |    1
      1  |   11:00   |    1
      1  |   12:00   |    0
      1  |   13:00   |    1
      2  |    2:00   |    1
      2  |    3:00   |    1
      2  |    4:00   |    0
      2  |    5:00   |    0
      3  |    8:00   |    0
      3  |    9:00   |    0
      4  |    1:00   |    1
      4  |    2:00   |    1
      5  |   16:00   |    0

What I want to do find out when each ID was inactive (active = 0) for how long. What I tried to do was group it by id when active = 0, and do a datediff on the min and max time. But that would give me a result for id 1 that said it was offline for 8 hours (12:00 – 4:00) @ 12:00. When I really want is a query that will give me the following result set.

    id | approx. offline in hours |  at time
    ---+--------------------------+-----------
     1 |            1             |    5:00
     1 |            2             |    9:00
     1 |            0             |   12:00
     2 |            1             |    5:00
     3 |            0             |    9:00
     5 |            0             |   16:00

The wrong query I initially tried is

SELECT id as [Inactive],
       DATEDIFF(hour, MIN(Timestamp), MAX(Timestamp)) as [approx. offline in hours],
       MAX(Timestamp) as [at time]
FROM table
WHERE active = 0
GROUP BY [Inactive]

But the problem with that query is that it skips the Active times in between. I’ve been looking at THIS question that’s been asked and answered using PARTITION, but it looks like the question is different enough and the answer is too specific to the question that I can’t make sense of it.

Any help is appreciated.

  • 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-17T05:15:45+00:00Added an answer on June 17, 2026 at 5:15 am

    One way to approach this, that works in any database, is to use a correlated subquery. The idea is to assign a groupname to each consecutive string of active values. The particular groupname is the time of the next change in value.

    select id, active, min(TimeStamp), max(TimeStamp)
    from (select t.*,
                 (select min(timeStamp) from t t2 where t2.id = t.id and t2.timeStamp > t.timeStamp and t2.active <> t.active
                 ) groupName
          from t
         ) t
    group by id, groupName, active
    

    One caveats how you get turn timestamps into durations depends on the database. Since you don’t specify the database, I’ll let you add that logic.

    Also, if the last record for a given id is inactive, the groupname is NULL. That is not a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I want use html5's new tag to play a wav file (currently only supported

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.