ServiceManager.Report.ServiceManagement.SP.GetOMGroupGroupData.Install (Resource)

Element properties:

TypeResource
File NameServiceManager.Report.ServiceManagement.SP.GetOMGroupGroupData.Install.sql
AccessibilityPublic

Source Code:

<Resource ID="ServiceManager.Report.ServiceManagement.SP.GetOMGroupGroupData.Install" Accessibility="Public" FileName="ServiceManager.Report.ServiceManagement.SP.GetOMGroupGroupData.Install.sql"/>

File Content: ServiceManager.Report.ServiceManagement.SP.GetOMGroupGroupData.Install.sql



IF EXISTS (SELECT * FROM sys.procedures WHERE name = N'ServiceManager_Report_ServiceManagement_SP_GetOMGroupGroupData')
BEGIN
DROP PROCEDURE dbo.ServiceManager_Report_ServiceManagement_SP_GetOMGroupGroupData
END
GO



CREATE PROCEDURE [dbo].[ServiceManager_Report_ServiceManagement_SP_GetOMGroupGroupData]
@StartDate datetime = NULL,
@EndDate datetime = NULL,
@LanguageCode nvarchar(max)= 'ENU',
@GroupByForReport nvarchar(max) = NULL,
@TimeAggregation nvarchar(max) = NULL,
@ServiceDimKeys nvarchar(max)= NULL,
@GroupDimKeys nvarchar(max)= NULL,
@CollectionDimKeys nvarchar(max)= NULL,
@CIDimKeys nvarchar(max) = NULL
AS
BEGIN
SET NOCOUNT ON

DECLARE @Error int
DECLARE @ExecError int

EXEC [dbo].[ServiceManager_Report_ServiceManagement_SP_GetServiceContainsComputersMap]
@StartDate = @StartDate,
@EndDate = @EndDate,
@ServiceDimKeys = @ServiceDimKeys,
@GroupByForReport = @GroupByForReport,
@TimeAggregation = @TimeAggregation,
@CIDimKeys = @CIDimKeys OUTPUT

EXEC [dbo].[ServiceManager_Report_ServiceManagement_SP_GetOMGroupContainsComputersMap]
@StartDate = @StartDate,
@EndDate = @EndDate,
@GroupDimKeys = @GroupDimKeys,
@GroupByForReport = @GroupByForReport,
@TimeAggregation = @TimeAggregation,
@CIDimKeys = @CIDimKeys OUTPUT

EXEC [dbo].[ServiceManager_Report_ServiceManagement_SP_GetCMCollectionContainsComputersMap]
@StartDate = @StartDate,
@EndDate = @EndDate,
@CollectionDimKeys = @CollectionDimKeys,
@GroupByForReport = @GroupByForReport,
@TimeAggregation = @TimeAggregation,
@CIDimKeys = @CIDimKeys OUTPUT

IF(@StartDate) is NULL SET @StartDate = '1900/01/01'
IF(@EndDate) is NULL SET @EndDate = '2100/01/01'
SET @EndDate = DATEADD(DAY, 1, @EndDate)

DECLARE @StartDateKey int
DECLARE @EndDateKey int
SET @StartDateKey = CONVERT(nvarchar(8), @StartDate, 112)
SET @EndDateKey = CONVERT(nvarchar(8), @EndDate, 112)

DECLARE @groupByComputers AS TABLE (
GroupById uniqueidentifier,
GroupByDimKey int,
GroupByBME uniqueidentifier,
GroupByEntityDimKey int,
GroupByName nvarchar(max),
ConfigItemDimKey int,
ConfigItemEntityDimKey int,
ConfigItemBME uniqueidentifier,
CreatedDate datetime,
DeletedDate datetime
)

--<CI SourceId="07CA4239-16FA-8C38-0F7E-30EB21B42CFA" ServiceDimKey="1" ServiceBME="6CF80856-BBE8-EDF9-2254-CFF6C9727F2F" ServiceEntityDimKey="64" ComputerDimKey="1" ComputerEntityDimKey="26" ComputerBME="A92B850D-53F5-8582-2113-5C20DCECE034" CreatedDate="2010-11-03T12:58:11.163" DeletedDate="2010-11-03T12:58:11.163"/>
DECLARE @ciListDoc XML = @CIDimKeys
INSERT INTO @groupByComputers
SELECT c.value('@GroupById[1]', 'uniqueidentifier'),
c.value('@GroupByDimKey[1]', 'int'),
c.value('@GroupByBME[1]', 'uniqueidentifier'),
c.value('@GroupByEntityDimKey[1]', 'int'),
c.value('@GroupByName[1]', 'nvarchar(max)'),
c.value('@ConfigItemDimKey[1]', 'int'),
c.value('@ConfigItemEntityDimKey[1]', 'int'),
c.value('@ConfigItemBME[1]', 'uniqueidentifier'),
c.value('@CreatedDate[1]', 'datetime'),
c.value('@DeletedDate[1]', 'datetime')
FROM @ciListDoc.nodes('/root/CI') AS T(c)

DECLARE @configItemDimKeys AS TABLE(ConfigItemEntityDimKey int, GroupByName nvarchar(max), DeletedDate datetime)

IF ( @ServiceDimKeys = '' AND @GroupDimKeys = '' AND @CollectionDimKeys = '' AND @GroupByForReport = 'AggregatedTime' )
BEGIN
INSERT INTO @configItemDimKeys
SELECT DISTINCT entityDim.EntityDimKey, configItem.DisplayName, '9999/01/01' AS DeletedDate
FROM dbo.EntityDimvw entityDim
INNER JOIN ConfigItemDimvw configItem on entityDim.BaseManagedEntityId = configItem.BaseManagedEntityId
WHERE configItem.IsDeleted = 0
END
ELSE
BEGIN
INSERT INTO @configItemDimKeys
SELECT DISTINCT dimGroupCfg.EntityDimKey, grp.GroupByName, '9999/01/01' AS DeletedDate
FROM dbo.ConfigItemDimvw dimGroupCfg
INNER JOIN @groupByComputers grp ON
dimGroupCfg.EntityDimKey = grp.GroupByEntityDimKey
UNION
SELECT DISTINCT entityDim.EntityDimKey, grp.GroupByName, ISNULL(grp.DeletedDate, '9999/01/01')
FROM dbo.ConfigItemDimvw dimGroupCfg
INNER JOIN @groupByComputers grp ON dimGroupCfg.BaseManagedEntityId = grp.ConfigItemBME
INNER JOIN dbo.EntityDimvw entityDim ON entityDim.BaseManagedEntityId = dimGroupCfg.BaseManagedEntityId
WHERE grp.CreatedDate <= @EndDate
AND ISNULL(grp.DeletedDate, @StartDate) >= @StartDate
END


IF @GroupByForReport != 'AggregatedTime'
BEGIN

DECLARE @summary AS TABLE(ReportSeries NVARCHAR(256),
Alerts int,
AlertsToIncidentsRatio FLOAT)

INSERT INTO @summary
SELECT targetEntities.GroupByName AS ReportSeries,
COUNT(DISTINCT AlertInstanceDimKey) AS Alerts,
null AS AlertsToIncidentsRatio
FROM dbo.AlertInstanceFactvw factAlerts
INNER JOIN dbo.AlertDimvw dimAlert on factAlerts.AlertDimKey = dimAlert.AlertDimKey
INNER JOIN @configItemDimKeys targetEntities ON dimAlert.TargetEntityDimKey = targetEntities.ConfigItemEntityDimKey AND targetEntities.DeletedDate >= factAlerts.TimeRaised
WHERE factAlerts.TimeRaised >= @StartDate AND factAlerts.TimeRaised < @EndDate
GROUP BY targetEntities.GroupByName


INSERT INTO @summary
SELECT targetEntities.GroupByName AS ReportSeries,
null as Alerts,
SUM(CASE WHEN dimInc.AlertId IS NULL THEN 0 ELSE 1 END) * 100.0/COUNT(DISTINCT AlertInstanceDimKey) AS AlertsToIncidentsRatio
FROM dbo.AlertInstanceFactvw factAlerts
INNER JOIN dbo.AlertDimvw dimAlert ON factAlerts.AlertDimKey = dimAlert.AlertDimKey
INNER JOIN @configItemDimKeys targetEntities ON dimAlert.TargetEntityDimKey = targetEntities.ConfigItemEntityDimKey AND targetEntities.DeletedDate >= factAlerts.TimeRaised
LEFT JOIN dbo.IncidentDimvw dimInc ON dimInc.AlertId = CONVERT(nvarchar(256),dimAlert.AlertProblemGuid)
WHERE factAlerts.TimeRaised >= @StartDate AND factAlerts.TimeRaised < @EndDate
GROUP BY targetEntities.GroupByName


SELECT
ReportSeries,
null As Incidents,
null As IncidentsResolvedWithinTarget,
null As AvgTimeToResolution,
SUM(Alerts),
SUM(AlertsToIncidentsRatio),
null As PhysicalComputers,
null As VirtualComputers,
null As SoftwareUpdateCompliance,
null As SecurityBulletinCompliance,
null As DCMIncidents,
null As TotalConsumption,
null As CostOfPower,
null As UnusedConsumption
from @summary
group by ReportSeries

END
ELSE IF @TimeAggregation IS NOT NULL
BEGIN

DECLARE @datesummary AS TABLE(ReportSeries DateTime,
Alerts int,
AlertsToIncidentsRatio INT)

DECLARE @selectdatedim AS TABLE(DateKey int,
CalendarDate DateTime,
DailyAggregate DateTime,
WeeklyAggregate DateTime,
MonthlyAggregate DateTime,
QuarterlyAggregate DateTime,
YearlyAggregate DateTime)


INSERT INTO @selectdatedim
Select
DateKey,
CalendarDate,
DailyAggregate = DD.CalendarDate,
WeeklyAggregate = DATEADD(DAY, (DD.WeekNumberInYear - 1)*7, CONVERT(nvarchar(4),DD.YearNumber) + '-1-1 00:00:00'),
MonthlyAggregate = DATEADD(MONTH, DD.MonthNumber - 1, CONVERT(nvarchar(4),DD.YearNumber) + '-1-1 00:00:00'),
QuarterlyAggregate = DATEADD(MONTH, (((DD.MonthNumber-1)/3) *3), CONVERT(nvarchar(4),DD.YearNumber) + '-1-1 00:00:00'),
YearlyAggregate = CONVERT(datetime, CONVERT(nvarchar(4),DD.YearNumber) + '-1-1 00:00:00')
from DateDim DD



INSERT INTO @summary
SELECT ReportSeries =
CASE
WHEN ( @TimeAggregation = 'Daily')
THEN selectdatedim.DailyAggregate
WHEN (@TimeAggregation = 'Weekly')
THEN selectdatedim.WeeklyAggregate
WHEN (@TimeAggregation = 'Monthly')
THEN selectdatedim.MonthlyAggregate
WHEN (@TimeAggregation = 'Quarterly')
THEN selectdatedim.QuarterlyAggregate
WHEN (@TimeAggregation = 'Yearly')
THEN selectdatedim.YearlyAggregate
ELSE 0
END,
COUNT(DISTINCT AlertInstanceDimKey) AS Alerts,
null AS AlertsToIncidentsRatio
FROM dbo.AlertInstanceFactvw factAlerts
INNER JOIN dbo.AlertDimvw dimAlert on factAlerts.AlertDimKey = dimAlert.AlertDimKey
INNER JOIN @configItemDimKeys targetEntities ON dimAlert.TargetEntityDimKey = targetEntities.ConfigItemEntityDimKey AND targetEntities.DeletedDate >= factAlerts.TimeRaised
INNER JOIN @selectdatedim selectdatedim ON selectdatedim.DateKey = factAlerts.DateKey
WHERE factAlerts.TimeRaised >= @StartDate AND factAlerts.TimeRaised < @EndDate
GROUP BY
CASE
WHEN ( @TimeAggregation = 'Daily')
THEN selectdatedim.DailyAggregate
WHEN (@TimeAggregation = 'Weekly')
THEN selectdatedim.WeeklyAggregate
WHEN (@TimeAggregation = 'Monthly')
THEN selectdatedim.MonthlyAggregate
WHEN (@TimeAggregation = 'Quarterly')
THEN selectdatedim.QuarterlyAggregate
WHEN (@TimeAggregation = 'Yearly')
THEN selectdatedim.YearlyAggregate
ELSE 0
END


INSERT INTO @summary
SELECT ReportSeries =
CASE
WHEN ( @TimeAggregation = 'Daily')
THEN selectdatedim.DailyAggregate
WHEN (@TimeAggregation = 'Weekly')
THEN selectdatedim.WeeklyAggregate
WHEN (@TimeAggregation = 'Monthly')
THEN selectdatedim.MonthlyAggregate
WHEN (@TimeAggregation = 'Quarterly')
THEN selectdatedim.QuarterlyAggregate
WHEN (@TimeAggregation = 'Yearly')
THEN selectdatedim.YearlyAggregate
ELSE 0
END,
null as Alerts,
SUM(CASE WHEN dimInc.AlertId IS NULL THEN 0 ELSE 1 END) * 100.0/COUNT(DISTINCT AlertInstanceDimKey) AS AlertsToIncidentsRatio
FROM dbo.AlertInstanceFactvw factAlerts
INNER JOIN dbo.AlertDimvw dimAlert ON factAlerts.AlertDimKey = dimAlert.AlertDimKey
INNER JOIN @configItemDimKeys targetEntities ON dimAlert.TargetEntityDimKey = targetEntities.ConfigItemEntityDimKey AND targetEntities.DeletedDate >= factAlerts.TimeRaised
LEFT JOIN dbo.IncidentDimvw dimInc ON dimInc.AlertId = CONVERT(nvarchar(256),dimAlert.AlertProblemGuid)
INNER JOIN @selectdatedim selectdatedim ON selectdatedim.DateKey = factAlerts.DateKey
WHERE factAlerts.TimeRaised >= @StartDate AND factAlerts.TimeRaised < @EndDate
GROUP BY
CASE
WHEN ( @TimeAggregation = 'Daily')
THEN selectdatedim.DailyAggregate
WHEN (@TimeAggregation = 'Weekly')
THEN selectdatedim.WeeklyAggregate
WHEN (@TimeAggregation = 'Monthly')
THEN selectdatedim.MonthlyAggregate
WHEN (@TimeAggregation = 'Quarterly')
THEN selectdatedim.QuarterlyAggregate
WHEN (@TimeAggregation = 'Yearly')
THEN selectdatedim.YearlyAggregate
ELSE 0
END


SELECT
ReportSeries,
null As Incidents,
null As IncidentsResolvedWithinTarget,
null As AvgTimeToResolution,
SUM(Alerts) AS Alerts ,
SUM(AlertsToIncidentsRatio) AS AlertsToIncidentsRatio,
null As PhysicalComputers,
null As VirtualComputers,
null As SoftwareUpdateCompliance,
null As SecurityBulletinCompliance,
null As DCMIncidents,
null As TotalConsumption,
null As CostOfPower,
null As UnusedConsumption
from @datesummary
group by ReportSeries
END




SET @Error = @@ERROR

QuitError:

RETURN @Error
END

GO


GRANT EXECUTE ON dbo.ServiceManager_Report_ServiceManagement_SP_GetOMGroupGroupData TO reportuser
GO