您好,我来自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
请帮忙吗?
答案 0 :(得分:4)
这是您标记为无效的部分:
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'
首先,将连接条件放在where
子句而不是on
子句中是非常错误的形式。所以,让我把它重写为:
select count(*) as [cnt]
from [myAlert1] aa join
[Alert] bb
on aa.AlertId = bb.id
where aa.EventType = EventType and
aa.EventId = id and
bb.DetectionAlertType = 'myAlert1'
如上所述,这被“解释为”:
select count(*) as [cnt]
from [myAlert1] aa join
[Alert] bb
on aa.AlertId = bb.id
where aa.EventType = aa.EventType and -- because there is no bb.EventType
aa.EventId = bb.id and -- because there is no aa.id
bb.DetectionAlertType = 'myAlert1'
EventId和bb.Id之间的链接可能会阻止任何行返回 - 或者至少会减少行数。
我怀疑EventType
和id
是PL / SQL变量。如果是这样,这些将写成:
select count(*) as [cnt]
from [myAlert1] aa join
[Alert] bb
on aa.AlertId = bb.id
where aa.EventType = @EventType and
aa.EventId = @id and
bb.DetectionAlertType = 'myAlert1'