鉴于这种:
with data_row as (select 1 as col_1 from dual)
select 'Y' as row_exists from dual where exists
(select null
from data_row
where col_1 in (2,1))
我怎样才能得到这个?
Col_1 Row_exists
--------------------
1 Y
2 N
鉴于这种:
with data_row as (select 1 as col_1 from dual)
select 'Y' as row_exists from dual where exists
(select null
from data_row
where col_1 in (2,1))
我怎样才能得到这个?
Col_1 Row_exists
--------------------
1 Y
2 N
为了得到一行输出,你需要一行输入。您想获得带有“2”的第二行,但没有具有该值的表。
该方法是生成一个包含所需值的表,然后使用left outer join它来查找匹配项:
with data_row as (
select 1 as col_1
from dual
),
what_i_care_about as (
select 1 as col from dual union all
select 2 from dual
)
select wica.col,
(case when dr.col_1 is NULL then 'N' else 'Y' end) as row_exists
from what_i_care_about wica left outer join
data_row dr
on wica.col = dr.col_1;
你不能直接做你想做的事——即为in列表中的每个缺失值创建一行。如果您有很多值并且它们是连续的数字,那么您可以使用connect byCTE 或递归 CTE 来生成这些值。