我有以下几个列的表格:
foo reportdate baar 01.04.16 1 baar_value b_value from_date to_date 1 01.01.16 01.01.17 1 01.01.15 01.01.16
我想要的逻辑有点复杂,但我试图找到一种方法来引用以下查询中baar_value-where子句中的表foo的reportdate:
SELECT *
FROM foo
WHERE baar IS NOT NULL
AND NOT EXISTS
(SELECT *
FROM baar_value
WHERE b_value = baar
AND reportdate
BETWEEN from_date AND to_date
)
如何检查foo.reportdate是否在baar_value.from_date和baar_value_to_date之间?
答案 0 :(得分:0)
根据您的描述,您可能只需要以下内容:
SELECT *
FROM foo
WHERE baar IS NOT NULL
AND NOT EXISTS
(SELECT *
FROM baar_value
WHERE b_value = baar
AND foo.reportdate BETWEEN from_date AND to_date
)
<强>测试强>
SQL> create table foo(reportdate, baar) as
2 select date '2016-04-01', 1 from dual;
Table created.
SQL> create table baar_value( b_value, from_date, to_date) as
2 select 1, date '2016-01-01', date '2017-01-01' from dual union all
3 select 1, date '2015-01-01', date '2016-01-01' from dual;
Table created.
SQL> SELECT *
2 FROM foo
3 WHERE baar IS NOT NULL
4 AND NOT EXISTS
5 (SELECT *
6 FROM baar_value
7 WHERE b_value = baar
8 AND foo.reportdate BETWEEN from_date AND to_date
9 );
no rows selected