2017-04-09 6 views
-1

の間、私はデータを以下のいる選択:私はどこの日ではないTABLE1からデータを選択する必要があり日ないで昼間とEND_DATE

Code code_id status  daytime     End_date 
F23 123df  down  16 Jul 2016 06:00 am 18 Jul 2016 08:00 pm 
F23 123df  down  19 Jul 2016 05:00 am 21 Jul 2016 03:45 pm 
F23 123df  down  23 Jul 2016 02:40 am 

表1:

Code code_id day 
F23 123df  16 Jul 2016 
F23 123df  17 Jul 2016 
F23 123df  18 Jul 2016 
F23 123df  19 Jul 2016 
F23 123df  20 Jul 2016 
F24 124df  16 Jul 2016 
F24 124df  17 Jul 2016 
F24 124df  18 Jul 2016 
F24 124df  19 Jul 2016 
F24 124df  20 Jul 2016 

表2をtable1の日('16 Jul 7月2016 'がtable2の'16 Jul 2016 06:00'と同じ日である場合)は、table2 day from daytimeやEnd_date fieldsのようなものですが、それでも選択する必要がありますデータこの日はEnd_dateがnullの場合は、sysdate-nvl(End_date、sysdate)でなければなりません。例えば

とき= '16 2016' 年7月日の選択から、それがなければならないTABLE1:

F23 123df  16 Jul 2016 
F24 124df  16 Jul 2016 

しかし、'17 2016' 年7月のための選択それがなければならない:

F24 124df  17 Jul 2016 
それがなければならない'18 2016' 年7月のための選択

F23 123df  18 Jul 2016 
F24 124df  18 Jul 2016 

おかげで、 S

答えて

0
SELECT * 
FROM table1 t 
WHERE NOT EXISTS (
     SELECT 1 
     FROM table2 x 
     WHERE t.code       = x.code 
     AND t.code_id      = x.code_id 
     AND t.day       >= x.daytime 
     AND t.day + INTERVAL '86399' SECOND <= x.end_date 
     ); 
関連する問題