-6

こんにちは、私は 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

何か助けてください。

4

1 に答える 1

4

This is the section that you have marked as not working:

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'

First, it is very bad form to put join conditions in the where clause rather than the on clause. So, let me rewrite this as:

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'

As written, this is "interpreted" as:

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'

The link between EventId and bb.Id would probably prevent any rows from returns -- or at least, you would get fewer rows.

I suspect that EventType and id are intended to be PL/SQL variables. If so, these would be written as:

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'
于 2012-12-27T15:19:19.010 に答える