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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:25+00:00 2026-05-23T09:14:25+00:00

I have a table I am trying to generate reports from. Its basically a

  • 0

I have a table I am trying to generate reports from.
Its basically a log of when something breaks (goes down), and then gets fixed.

Table schema and some example data is below.
To illustrate:
1 row is inserted when it goes down
1 row is inserted when it comes back up.

What I am trying to do is report on various aspects, things like:
Amount of downtime in a given day / week / month
Number of times its gone down in a given day / week / month.

Ideally in a way that would easily be exported to excel or something similar to be graphed.

I’m having trouble coming up with any kind of queries to get this info.

I have this one for example:

SELECT [Name], datepart(day,[Inserted]), count([SystemDown])     
FROM [DownTimeLog]
WHERE [SystemDown]=1
GROUP BY [Name],datepart(day,[Inserted]) 

which gives me the number of times the system has gone down per day, which is a good starting point.

But i’m trying to come up with a way of showing the total time its been down, but I am drawing a blank.
Some days for example may be 0, sometimes it may go down a few times, so trying to sum the time difference between 2 corresponding rows is provinh tough.

CREATE TABLE [dbo].[DownTimeLog]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] VARCHAR(30) NOT NULL,
    [SystemDown] BIT NOT NULL,
    [Inserted] DATETIME NOT NULL
)

INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 14 2011  1:49:58:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 14 2011  2:49:58:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 15 2011  1:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 15 2011  2:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 15 2011  4:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 15 2011  5:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 17 2011  1:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 17 2011  3:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 18 2011 10:00:00:000AM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 18 2011 11:00:00:000AM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 18 2011  1:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 18 2011  3:30:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 18 2011  4:00:00:000PM')
INSERT INTO [DownTimeLog] ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 18 2011  8:00:00:000PM')

So for example using the data above I’d like to pull data back that was something like:

System1 | June 14 2011 | 1 hour | 1 occurence
System1 | June 15 2011 | 2 hours | 2 occurence’s
System1 | June 16 2011 | 0 hours | 0 occurence’s
System1 | June 17 2011 | 2 hours | 1 occurence
System1 | June 18 2011 | 7.5 hours | 3 occurence’s

I hope someone can give me a method of doing what I am trying to do.

—
Edit:

Thanks all for some great answers. Helped me out a tonne.
I thought my sql was pretty string, never heard of a cross apply though – guess i need to go back to school!

Cheers!

  • 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-05-23T09:14:25+00:00Added an answer on May 23, 2026 at 9:14 am

    The following will produce your exact output including missing days. Note, this code is based on Mikael Eriksson’s answer.

    CREATE TABLE #DownTimeLog
    (
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [Name] VARCHAR(30) NOT NULL,
        [SystemDown] BIT NOT NULL,
        [Inserted] DATETIME NOT NULL
    )
    
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 14 2011  1:49:58:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 14 2011  2:49:58:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 15 2011  1:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 15 2011  2:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 15 2011  4:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 15 2011  5:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 17 2011  1:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 17 2011  3:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 18 2011 10:00:00:000AM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 18 2011 11:00:00:000AM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 18 2011  1:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 18 2011  3:30:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',1,'Jun 18 2011  4:00:00:000PM')
    INSERT INTO #DownTimeLog ([Name],[SystemDown],[Inserted])VALUES('System1',0,'Jun 18 2011  8:00:00:000PM')
    
    CREATE TABLE #DownTimeLogModified (Name nvarchar(512), [Date] nvarchar(512), DownTime int,  Occurrences int)
    
    INSERT INTO #DownTimeLogModified (Name, [Date], DownTime,  Occurrences)
    select D1.Name,
           CONVERT(VARCHAR(12), dateadd(d, datediff(d, 0, D1.Inserted), 0), 107) as [Date],
           sum(datediff(mi, D1.Inserted, D2.Inserted)) as DownTime,
           count(*) as Occurrences
    from #DownTimeLog as D1
      cross apply ( select top 1 Name,
                                 Inserted
                    from #DownTimeLog
                    where SystemDown = 0 and
                          Name = D1.Name and
                          Inserted > D1.Inserted 
                    order by Inserted             
                  ) as D2
    where D1.SystemDown = 1
    group by D1.Name, dateadd(d, datediff(d, 0, D1.Inserted), 0)
    
    DECLARE @startdate datetime,@enddate datetime
    select @startdate = MIN(Inserted) FROM #DownTimeLog
    select @enddate = MAX(Inserted) FROM #DownTimeLog
    
    ;WITH DateIntervalsCTE AS
            (
            SELECT 1 i,@startdate AS Date
            UNION ALL
            SELECT i + 1, DATEADD(day, i, @startdate )
            FROM DateIntervalsCTE 
            WHERE DATEADD(day, i, @startdate ) <= @enddate
            )
    
    SELECT CASE WHEN a.Name is null THEN 'No System downtime' ELSE a.Name END as Name,CONVERT(VARCHAR(12), b.Date, 107) AS Date,CASE WHEN a.DownTime is null THEN 0 ELSE a.DownTime END AS DownTime,CASE WHEN a.Occurrences is null THEN 0 ELSE a.Occurrences END AS Occurrences 
    FROM DateIntervalsCTE b
    LEFT JOIN #DownTimeLogModified a ON a.Date = CONVERT(VARCHAR(12), b.Date, 107)
    
    DROP TABLE #DownTimeLog
    DROP TABLE #DownTimeLogModified
    

    Output

    Name                           Date                    DownTime    Occurrences
    ------------------------------ ----------------------- ----------- -----------
    System1                        Jun 14, 2011                60           1
    System1                        Jun 15, 2011               120           2
    No system downtime             Jun 16, 2011                 0           0
    System1                        Jun 17, 2011               120           1
    System1                        Jun 18, 2011               450           3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to have table borders in my PDF using pisa to generate the
I'm trying to generate reports. I already created the HTML table in a view
I'm trying to generate a tree structure from a table in a database. The
While trying to use LINQ to SQL I encountered several problems. I have table
I am trying to have a table header, in a seperate div stay in
I have a table column I’m trying to expand and hide. jQuery seems to
I have a table with users and are trying to get a list with
I have a table that has redundant data and I'm trying to identify all
I have a table containing hundreds of entries and I am trying to delete
I have a table, trying to figure out how to navigate to the 3.35

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.