0

複数のイベント タイプを持つ複数の行を持つ 1 つのテーブルがあります。

すべての開始イベントには終了イベントがあります。設計上の選択により、開始イベントの列には終了イベントの ID がありますが、その逆はありません。同様に、開始理由は終了理由と同じではありません。

私のコラムは次のとおりです。

eventTYPE - the type of event (start, end, terminate etc)
eventID - unique ID
eventDate - date the event will happen
endEvent - ID of end event (if event type <> start - this will be NULL)
reason - Reason for the event

私はすべての eventType=END を返す必要があります。これは、比較対象の eventType=START の理由がユーザー指定の日付の特定の理由ではないことです。

次の 3 つのステートメントを使用する必要があることはわかっていますが、それらを組み合わせる方法がわかりません。

SELECT eventId 
FROM Events
WHERE eventType='END' and eventDate='<USER SPECIFIED>'

SELECT endEvent    
FROM Events
WHERE eventType='START' and reason <> 'mgp' and endEvent='<ID FROM ABOVE>'

SELECT * 
FROM dbo.Events 
WHERE eventType='END' and eventId='<END EVENT FROM ABOVE>'

どんな助けでも大歓迎です!

4

2 に答える 2

2

クエリは次のようなものです。

select *
from events se left outer join
     events ee
     on se.endevent = ee.eventid and se.eventtype = 'start' and ee.eventtype = 'end'
where ee.eventdate = '<USER SPECIFIED>' and
       se.reason <> 'mgp'

これは、開始レコードと終了レコードを一致させてイベントを結合し、その結果にロジックを適用するだけです。

于 2013-02-15T01:13:33.727 に答える
0

どうですか、

SELECT * 
FROM dbo.Events 
WHERE eventType='END' and eventId IN (

   SELECT endEvent    
   FROM Events
   WHERE eventType='START' and reason <> 'mgp' and endEvent IN (

      SELECT eventId 
      FROM Events
      WHERE eventType='END' and eventDate='<USER SPECIFIED>'

   )
)
于 2013-02-15T01:13:09.700 に答える