P
DECLARE @start_date DATETIME
SET @now = GETUTCDATE()
SET @start_date = DATEADD(MONTH, DATEDIFF(MONTH, 0, DATEADD(MONTH, -1 * @num_historical_months, @now)), 0)
DECLARE @month_string NVARCHAR(max)
SELECT @month_string = ISNULL(@month_string + ', [' + CAST(MonthYear AS NVARCHAR(max)) + ']', '[' + CAST(MonthYear AS NVARCHAR(max)) + ']')
FROM
(
SELECT DATENAME(MONTH, DATEADD(MONTH, x.number, @start_date)) + ' ' + DATENAME(YEAR, DATEADD(MONTH, x.number, @start_date)) As MonthYear
FROM master.dbo.spt_values x
WHERE x.type = 'P'
AND x.number <= DATEDIFF(MONTH, @start_date, @now)
) AS T
DECLARE @query NVARCHAR(max)
SET @query = N'
SELECT #slas_to_report_on.Name, ' + @month_string + '
FROM
(
SELECT *
FROM
(
SELECT DATENAME(MONTH, T.DateTime) + '' '' + DATENAME(YEAR, T.DateTime) As Month,
T.ServiceLevelObjectiveGuid,
(T.InGoodStateMilliseconds/(T.IntervalDurationMilliseconds+1))*100 As AvailabilityPercentage
FROM
(
SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, DateTime), 0) As DateTime
,ServiceLevelObjectiveGuid
,SUM(IntervalDurationMilliseconds) As IntervalDurationMilliseconds
,SUM(InGoodStateMilliseconds) As InGoodStateMilliseconds
FROM
(
SELECT DISTINCT vState.DateTime As DateTime
,vServiceLevelObjective.ServiceLevelObjectiveGuid
,vState.IntervalDurationMilliseconds + 0.0 As IntervalDurationMilliseconds
,(vState.IntervalDurationMilliseconds + 0.0
- vState.InRedStateMilliseconds
- vMonitorServiceLevelObjectiveManagementPackVersion.YellowStateUndesiredInd * vState.InYellowStateMilliseconds
- vMonitorServiceLevelObjectiveManagementPackVersion.DisabledStateUndesiredInd * vState.InDisabledStateMilliseconds
- vMonitorServiceLevelObjectiveManagementPackVersion.UnplannedMaintenanceStateUndesiredInd * vState.InUnplannedMaintenanceMilliseconds
- vMonitorServiceLevelObjectiveManagementPackVersion.PlannedMaintenanceStateUndesiredInd * vState.InPlannedMaintenanceMilliseconds
- vMonitorServiceLevelObjectiveManagementPackVersion.HealthServiceUnavailableStateUndesiredInd * vState.HealthServiceUnavailableMilliseconds
) As InGoodStateMilliseconds
FROM #slas_to_report_on
INNER JOIN vServiceLevelObjective ON vServiceLevelObjective.ServiceLevelObjectiveGuid = #slas_to_report_on.ServiceLevelObjectiveGuid
INNER JOIN vManagedEntity ON vManagedEntity.ManagedEntityGuid = #slas_to_report_on.ManagedEntityGuid
INNER JOIN vServiceLevelObjectiveManagementPackVersion ON vServiceLevelObjectiveManagementPackVersion.ServiceLevelObjectiveRowId = vServiceLevelObjective.ServiceLevelObjectiveRowId
INNER JOIN vMonitorServiceLevelObjectiveManagementPackVersion ON vMonitorServiceLevelObjectiveManagementPackVersion.ServiceLevelObjectiveManagementPackVersionRowId = vServiceLevelObjectiveManagementPackVersion.ServiceLevelObjectiveManagementPackVersionRowId
INNER JOIN vManagedEntityMonitor ON vManagedEntityMonitor.MonitorRowId = vMonitorServiceLevelObjectiveManagementPackVersion.MonitorRowId AND vManagedEntityMonitor.ManagedEntityRowId = vManagedEntity.ManagedEntityRowId
INNER JOIN vStateDailyFull as vState ON vState.DateTime >= @start_date AND vState.DateTime <= @now AND vState.ManagedEntityMonitorRowId = vManagedEntityMonitor.ManagedEntityMonitorRowId
) As T
GROUP BY DATEADD(MONTH, DATEDIFF(MONTH, 0, DateTime), 0), ServiceLevelObjectiveGuid
) AS T
) AS T
PIVOT
(
SUM(AvailabilityPercentage)
FOR [Month] IN (' + @month_string + ')
) AS pvt
)

