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!