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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:17:15+00:00 2026-05-10T17:17:15+00:00

I have a table which contains my server status create table ServerStatus ( ServerId

  • 0

I have a table which contains my server status

create table ServerStatus (     ServerId int,      StartTime datetime,      Seconds int,     [State] char(8)  ) 

I would like a query that given a start and end date will summarize the time the server spends in each state during that time. I would also like the query to return the amount of time the servers spend in an unknown state.

So, for example for the following data

1   2008-01-01 00:00:00.000 120 broken   1   2008-01-02 00:00:00.000 120 off      1   2008-01-03 00:00:00.000 240 burning  1   2008-01-04 00:00:00.000 60  off      1   2008-01-05 00:00:00.000 60  off      2   2008-01-01 00:00:00.000 60  broken   2   2008-01-02 00:00:00.000 30  off      2   2008-01-03 00:00:00.000 20  burning  2   2008-01-04 00:00:00.000 600 off      3   2007-01-04 00:00:00.000 600 off      4   2007-12-12 00:00:00.000 999999999   onfire   

Provided the range.

select  @start = dateadd(second, 60, '2008-01-01'),  @fin = dateadd(second, 60, '2008-01-04') 

I would like to return the results:

1   broken      60 1   burning     240 1   off         180 1   unknown     258720 2   burning     20 2   off         90 2   unknown     259090 4   onfire      259200 

This question is somewhat related to: Combining split date ranges in a SQL query

  • 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. 2026-05-10T17:17:16+00:00Added an answer on May 10, 2026 at 5:17 pm

    This is the best I have come up with so far:

    declare @start datetime declare @fin datetime   select  @start = dateadd(second, 60, '2008-01-01'), @fin = dateadd(second, 60, '2008-01-04')  select ServerId, State, Total = sum(     case when StartTime > @start and DateAdd(second, Seconds, StartTime) <= @fin         then Seconds     when StartTime < @start and DateAdd(second, Seconds, StartTime) <= @fin         then DateDiff(second, @start, DateAdd(second, Seconds, StartTime))      when StartTime < @start and DateAdd(second, Seconds, StartTime) >= @fin         then DateDiff(second, @start, @fin)     else          DateDiff(second,StartTime,@fin)     end) into #t from ServerStatus  where @start < dateadd(second, Seconds, StartTime)  and @fin > StartTime group by ServerId, State  insert #t select ServerId, 'unknown', DateDiff(second, @start, @fin) - sum(Total)  from #t  group by ServerId having DateDiff(second, @start, @fin) > sum(Total)  select * from #t  order by ServerId, State 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer What you might be thrown off by is that web… May 11, 2026 at 12:56 pm
  • added an answer That's a foreign key constraint. ALTER TABLE student ADD CONSTRAINT… May 11, 2026 at 12:56 pm
  • added an answer Apple has shared their Sync Services Framework it is documented… May 11, 2026 at 12:56 pm

Related Questions

In a SQL server database, I have a table which contains a TEXT field
I have a contacts table which contains fields such as postcode , first name
I have a database structure that has a Person table which contains fields such
I have a table named categories, which contains ID(long), Name(varchar(50)), parentID(long), and shownByDefault(boolean) columns.
I have an Excel Spreadsheet that contains all my data that I need to
The current project I inherited mainly revolves around one unnormalized table. There are some
I have a table that contains maybe 10k to 100k rows and I need
I have a unit test which contains the following line of code Site.objects.get(name=UnitTest).delete() and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.