Следующий запрос должен вернуть все ненулевые значения для столбца n.c
, так как C
не может иметь значение NULL.
select distinct A, B, a.C, n.C
from o
outer apply ( -- Get first C which Status = 1, if there is any
select C
from o i where i.A = o.A and i.B = o.B and STATUS = 1 and ROWNUM = 1) a
outer apply ( -- Get the latest C otherwise (ID is identity column)
select C
from o i where i.A = o.A and i.B = o.B and ROWNUM = 1
order by ID desc) n
Однако, n.C
будет нулевым, если a.C
будет нулевым.И он вернет желаемое значение, если я уберу внешнее применение a
.
select distinct A, B, n.C
from o
outer apply (
select C
from o i where i.A = o.A and i.B = o.B and ROWNUM = 1
order by ID desc) n
Это ошибка Oracle?
Кстати, он работает, как и ожидалось, если я переключаю два outer apply
?
select distinct A, B, a.C, n.C
from o
outer apply (
select C
from o i where i.A = o.A and i.B = o.B and ROWNUM = 1
order by ID desc) n
outer apply (
select C
from o i where i.A = o.A and i.B = o.B and STATUS = 1 and ROWNUM = 1) a
Версия Oracle
Oracle Database 12c Standard Edition Release 12.1.0.2.0 - 64bit Production
PL/SQL Release 12.1.0.2.0 - Production
CORE 12.1.0.2.0 Production
TNS for Linux: Version 12.1.0.2.0 - Production
NLSRTL Version 12.1.0.2.0 - Production
Тестовые данные
CN
из 2
, 2
равно нулю:
with o(ID, A, B, C, Status) as (
select 1, 1, 1, 1, 1 from dual union all
select 2, 2, 2, 2, 0 from dual union all
select 3, 2, 2, 2, 0 from dual
)
select distinct A, B, a.C Ca, n.C Cn
from o
outer apply ( -- Get first C which Status = 1, if there is any
select C
from o i where i.A = o.A and i.B = o.B and STATUS = 1 and ROWNUM = 1) a
outer apply ( -- Get the latest C otherwise (ID is identity column)
select C
from o i where i.A = o.A and i.B = o.B and ROWNUM = 1
order by ID desc) n
возвращает
A B CA CN
1 1 1 1
2 2 NULL NULL
CN
из 2
, 2
нетnull после перемещения outer apply (...) n
:
with o(ID, A, B, C, Status) as (
select 1, 1, 1, 1, 1 from dual union all
select 2, 2, 2, 2, 0 from dual union all
select 3, 2, 2, 2, 0 from dual
)
select distinct A, B, a.C Ca, n.C Cn
from o
outer apply ( -- Get the latest C otherwise (ID is identity column)
select C
from o i where i.A = o.A and i.B = o.B and ROWNUM = 1
order by ID desc) n
outer apply ( -- Get first C which Status = 1, if there is any
select C
from o i where i.A = o.A and i.B = o.B and STATUS = 1 and ROWNUM = 1) a
возвращает
A B CA CN
1 1 1 1
2 2 NULL 2
И следующий запрос (который пытается сделать ROWNUM более решительным) все же дал неверный результат.
with o(ID, A, B, C, Status) as (
select 1, 1, 1, 1, 1 from dual union all
select 2, 2, 2, 2, 0 from dual union all
select 3, 2, 2, 2, 0 from dual
)
select distinct A, B, a.C Ca, n.C Cn
from o
outer apply ( -- Get first C which Status = 1, if there is any
select C
from o i where i.A = o.A and i.B = o.B and STATUS = 1 and ROWNUM = 1) a
outer apply ( -- Get the latest C otherwise (ID is identity column)
select * from (
select C
from o i where i.A = o.A and i.B = o.B
order by ID desc) x
where ROWNUM = 1) n