0

2 つの日付フィールドがあり、where 句で OR を使用しようとしています。例として、

Select customer_records.customer_id, customer_records.join_date, rejected_customers.rejected_date, status_lookup.status_description 
From customer_records, rejected_customers, status_lookup
Where status_lookup.status_id(+) = customer_records.status_id and customer_records.customer_id = rejected_customers.customer_id
and (customer_records.join_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy') or rejected_customers.rejected_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')

したがって、基本的な結果は、参加日または拒否された日付が日付フィールドに該当する場合、customer_id が必要です。私はそれを機能させることができないようです、助けていただければ幸いです、ありがとう!

4

2 に答える 2

0

うまく動かないようだと言うときは、クエリを実行しても期待どおりの結果が得られず、エラーも発生しないと思います。場合によっては、一部の日付フィールドが時間の端数とともにデータベースに保存されます。そのため、「trunc」関数を使用する必要があります。Oracle データベースで次の例を参照してください。

SQL> select to_char(sysdate,  'dd/mm/yyyy hh:mi:ss') date_no_trunc from dual;

DATE_NO_TRUNC
-------------------
28/03/2013 10:04:27

SQL> select to_char(trunc(sysdate),  'dd/mm/yyyy hh:mi:ss') date_with_trunc from dual;

DATE_WITH_TRUNC
-------------------
28/03/2013 12:00:00

SQL> 

したがって、クエリを次のように変更する必要があります。

Select c.customer_id,
       c.join_date,
       r.rejected_date
From   customer_records c,
       rejected_customers r
Where  c.customer_id = r.customer_id
and   ( trunc(c.join_date) between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')
        or
        trunc(r.rejected_date) between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')
      )

また、長いテーブル名のエイリアスを与えることは常に良い考えです。これにより、クエリが短くなり、メモリの使用量が少なくなります。

于 2013-03-28T22:07:52.190 に答える
0
Select cr.customer_id, cr.join_date, rej.rejected_date
from customer_records cr left join rejected_customers rej on cr.customer_id=reg.customer_id
where (cr.join_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy')) or (rej.rejected_date between to_date('01-10-2012','dd-mm-yyyy') and to_date('01-11-2012','dd-mm-yyyy'))
于 2013-03-28T21:08:14.703 に答える