I have a line chart/graph that I need to include for all service outages we went through in the year. Currently, my SQL can pull data that includes any tickets creating in a ticketing system for these services.
However, if no ticket was created for a service, the service will not show in the rows or the service will show but not have a 100% Uptime for the months where no tickets were created for it.
Example of what I am looking for:
MONTH Service1 Service2 Service3
1 100% 99.7% 100%
2 99.8% 100% 96.5%
3 100% 99.8% 100%
But what it looks like is this:
MONTH Service1 Service2 Service3
1 99.7%
2 99.8% 96.5%
3 99.8%
The services get pulled by using a WHERE [Resource]='Affected Service'
so they are dynamically brought into the table, but no data is pulled for the service if no ticket was created in that month.
Current SQL Coding:
WITH rslt (ResourceID, YearNumber, MonthNumber, AvailableMinutes, DowntimeMinutes) AS (
SELECT
ore.ResourceID,
DATEPART(yyyy, ipr.OpenDate_MST) YearNumber,
DATEPART(mm, ipr.OpenDate_MST) MonthNumber,
MAX(CASE
WHEN DATEADD(MONTH, DATEDIFF(MONTH, -1, ipr.OpenDate_MST), -1) = DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1)
THEN (DATEPART(DD, GETDATE()) * 1440.0)
ELSE (DATEPART(DD, DATEADD(MONTH, DATEDIFF(MONTH, -1, ipr.OpenDate_MST), -1)) * 1440.0)
END) AvailableMinutes,
ISNULL(SUM(DATEDIFF(mi, ipr.OutageStartTime, ipr.OutageEndTime)), 0) DowntimeMinutes
FROM
vIncidentProblemRequest ipr
INNER JOIN vOwnedResource ore ON ore.ResourceID = ipr.AffectedServiceID
WHERE
CONVERT(DATETIME, CONVERT(CHAR(10), ipr.OpenDate_MST, 101)) >= '1/1/2013 12:00:00'
AND CONVERT(DATETIME, CONVERT(CHAR(10), ipr.OpenDate_MST, 101)) <= '12/31/2013 11:59:59'
GROUP BY
ore.ResourceID,
DATEPART(yyyy, ipr.OpenDate_MST),
DATEPART(mm, ipr.OpenDate_MST),
),
rslt2 (ResourceID, Application, ResourceClass, YearNumber, MonthNumber, AvailableMinutes, DowntimeMinutes, UptimePercent) AS (
SELECT
ore.ResourceID,
ore.ResourceName Application,
ore.ResourceClass,
rslt.YearNumber,
rslt.MonthNumber,
rslt.AvailableMinutes,
ISNULL(rslt.DowntimeMinutes, 0) DowntimeMinutes,
CASE
WHEN rslt.DowntimeMinutes IS NULL
THEN 1.0
ELSE ((rslt.AvailableMinutes - rslt.DowntimeMinutes)/rslt.AvailableMinutes)
END UptimePercent
FROM
vOwnedResource ore
LEFT OUTER JOIN rslt ON rslt.ResourceID = ore.ResourceID
WHERE
ore.ResourceClass = 'Enterprise Service')
select
MIN(DATEPART(yyyy, d.Date)) Year,
MIN(DATEPART(mm, d.Date)) MonthNum,
SUBSTRING(MIN(DATENAME(mm, d.Date)), 1, 3) Month,
r.Application,
r.ResourceClass,
CASE
WHEN r.UptimePercent IS NULL
THEN 1.0
ELSE r.UptimePercent
END UptimePercent
FROM
DimDate d
INNER JOIN rslt2 r ON r.YearNumber = datepart(yyyy, d.Date) AND r.MonthNumber = datepart(mm, d.Date)
WHERE
d.Date >= '1/1/2013 12:00:00'
AND d.Date <= '12/31/2013 11:59:59'
GROUP BY
datepart(yyyy, d.Date),
datepart(mm, d.Date),
DATENAME(mm, d.Date),
r.Application,
r.ResourceClass,
r.UptimePercent
ORDER BY
4,1,2