您好,我来自 Oracle 世界,我在 SQL Server 方面没有太多经验。有人能告诉我为什么这个查询的内部部分不起作用吗?我尝试了不同的选项,但它们似乎都不起作用:
select EventType, sum(Executed), sum(TriggeredScenarios), sum(OpenAlerts),sum(TotalAlerts)
from (
select id,
'myEvent' as [EventType],
TriggeredScenarios,
Executed,
TotalAlerts = (select sum(cnt) as [TotalCount]
-- it breaks from here
from (select count(*) as [cnt]
from [myAlert1] aa, [Alert] bb
where aa.EventType = EventType
and aa.EventId = id
and aa.AlertId = bb.id
and bb.DetectionAlertType = 'myAlert1'
-- until here
UNION
select count(*) as [cnt]
from [myAlert2] aa, [Alert] bb
where aa.EventType = EventType
and aa.EventId = id
and aa.AlertId = bb.id
and bb.DetectionAlertType = 'myAlert2'
) aa),
OpenAlerts = (select sum(cnt) as [TotalAlerts]
from ( select count(*) as [cnt]
from [Alert] aa, [myAlert1] bb
where aa.currentstateid not in (select intStateID
from _AlertStates
where strGroupName like (N'AlertsClosed%')
)
and bb.EventType = EventType
and bb.EventId = id
and bb.AlertId = aa.id
and aa.DetectionAlertType = 'myAlert1'
UNION
select count(*) as [cnt]
from [Alert] aa, [myAlert2] bb
where aa.currentstateid not in (select intStateID
from _AlertStates
where strGroupName like (N'AlertsClosed%')
)
and bb.EventType = EventType
and bb.EventId = id
and bb.AlertId = aa.id
and aa.DetectionAlertType = 'myAlert2'
) aa )
from [myEvent]
where [Timestamp] >= '11/26/2012'
AND [Timestamp] < '11/27/2012'
UNION
select id,
'myEvent2' as [EventType],
TriggeredScenarios,
Executed,
TotalAlerts = (select sum(cnt) as [TotalCount]
from (select count(*) as [cnt]
from [myAlert1] aa, [Alert] bb
where aa.EventType = EventType
and aa.EventId = id
and aa.AlertId = bb.id
and bb.DetectionAlertType = 'myAlert1'
UNION
select count(*) as [cnt]
from [myAlert2] aa, [Alert] bb
where aa.EventType = EventType
and aa.EventId = id
and aa.AlertId = bb.id
and bb.DetectionAlertType = 'myAlert2'
) aa),
OpenAlerts = (select sum(cnt) as [TotalAlerts]
from ( select count(*) as [cnt]
from [Alert] aa, [myAlert1] bb
where aa.currentstateid not in (select intStateID
from _AlertStates
where strGroupName like (N'AlertsClosed%')
)
and bb.EventType = EventType
and bb.EventId = id
and bb.AlertId = aa.id
and aa.DetectionAlertType = 'myAlert1'
UNION
select count(*) as [cnt]
from [Alert] aa, [myAlert2] bb
where aa.currentstateid not in (select intStateID
from _AlertStates
where strGroupName like (N'AlertsClosed%')
)
and bb.EventType = EventType
and bb.EventId = id
and bb.AlertId = aa.id
and aa.DetectionAlertType = 'myAlert2'
) aa )
from [myEvent2]
where [Timestamp] >= '11/26/2012'
AND [Timestamp] < '11/27/2012'
)
group by EventName
请问有什么帮助吗?