CREATE VIEW NBarAlertView AS
SELECT r.server_addr
,r.probe_addr
,r.rsrcIndex
,r.rsrcOtherIndex
,r.rsrcName
,r.userId
,AllStatsHCInBytes=case WHEN a.fieldIndex=12701 THEN 100*SUM(alertDuration*severity/5.0)/(24*3600) ELSE 0 END
,AllStatsHCOutBytes=case WHEN a.fieldIndex=12702 THEN 100*SUM(alertDuration*severity/5.0)/(24*3600) ELSE 0 END
,availability=CASE WHEN a.fieldIndex =1602 THEN 100*SUM(a.alertDuration*1.0)/(24*3600) ELSE 0 END
FROM ResourceNameNBarView r, AlertView a
WHERE r.server_addr=a.server_addr
AND r.probe_addr=a.probe_addr
AND r.rsrcIndex=a.rsrcIndex
AND r.rsrcOtherIndex=a.rsrcOtherIndex
AND a.severity > 0
AND a.severity <=5
AND r.monitored=1
AND a.IntervalDateTime BETWEEN dateadd(second, -datepart(second, getdate()),
dateadd(minute, -datepart(minute, getdate()),
dateadd(hour, -datepart(hour, getdate()),
dateadd(day, -1, getdate()))))
AND dateadd(second, -datepart(second, getdate()),
dateadd(minute, -datepart(minute, getdate()),
dateadd(hour, -datepart(hour, getdate()),
getdate())))
AND a.fieldIndex/100=127
GROUP BY r.server_addr
,r.probe_addr
,r.rsrcIndex
,r.rsrcOtherIndex
,r.rsrcName
,r.userId
,a.fieldIndex
GO
GRANT SELECT ON [dbo].[NBarAlertView] TO [uniadmin]
GO