MichaelとGordonが行ったように、空の行をでタックすることができますが、 :union allの前にそれを持っている必要があります。order by
...
and to_date(to_char(t.enddatetime, 'DD-MON-YYYY')) <=
to_date('?DATE2::?','MM/DD/YYYY')
union all
select null, null, null, null, null, null, null, null
from dual
order by eventid, starttime, actionsequence;
...選択された値ではないためcase、Gordonが直接持っていたものを使用することはできませんorder by-ORA-07185を取得します。(の列名は、テーブルの列ではなく、で割り当てorder byたエイリアスであることに注意してselectください。テーブル名/エイリアスは含めないでください。またnull、ユニオン部分の列にエイリアスを付ける必要はありません。ただし、わかりやすくするために必要な場合があります)。
ただし、これは実際の値の後にソートされることに依存しnullます。これは常に当てはまるとは限りません(確かではありませんが、NLSパラメーターの影響を受ける可能性がありますeventkey)null。したがって、クエリの両方の部分にダミー列を導入し、それを順序付けに使用する方がおそらく安全ですが、クエリをネストして結果から除外します。
select crewactionfactid, crewkey, eventid, actionsequence, type,
starttime, endtime, duration
from (
select 0 as dummy_order_field,
t.crewactionfactid,
t.crewkey,
t.eventkey as eventid,
t.actionsequence,
case t.actiontype
when 'DISPATCHED' then '2-Dispatched'
when 'ASSIGNED' then '1-Assigned'
when 'ENROUTE' then '3-Enroute'
when 'ARRIVED' then '4-Arrived'
else 'unknown'
end as type,
t.startdatetime as starttime,
t.enddatetime as endtime,
t.duration
from schema_name.table_name t
where to_date(to_char(t.startdatetime, 'DD-MON-YYYY')) >=
to_date('?DATE1::?','MM/DD/YYYY')
and to_date(to_char(t.enddatetime, 'DD-MON-YYYY')) <=
to_date('?DATE2::?','MM/DD/YYYY')
union all
select 1, null, null, null, null, null, null, null, null
from dual
)
order by dummy_order_field, eventid, starttime, action sequence;
ただし、日付の処理、特にto_date(to_char(...))パーツは奇妙です。時間の部分を失おうとしているようです。その場合は、trunk代わりに次を使用できます。
where trunc(t.startdatetime) >= to_date('?DATE1::?','MM/DD/YYYY')
and trunc(t.enddatetime) <= to_date('?DATE2::?','MM/DD/YYYY')
ただし、日付列に関数を適用すると、そのインデックスが使用されなくなるため、それをそのままにして、比較のために変数部分を適切な状態にすることをお勧めします。
where t.startdatetime >= to_date('?DATE1::?','MM/DD/YYYY')
and t.enddatetime < to_date('?DATE2::?','MM/DD/YYYY') + 1
は+ 1日を追加するので、idDATE2は、、07/12/2012フィルターは< 2012-07-13 00:00:00、と同じ<= 2012-07-12 23:59:59です。