0

PL/SQLを使用せずにそのようなリクエストのクエリを作成する方法はありますか:

if (select count(column) from table = 0)
then select column1, column2, column 3 from table where condition1
else select column1, column2, column 3 from table where condition2
4

3 に答える 3

0

2 つの LEFT JOINS と COALESCE を使用できます (ここでは、column1 > 2 / column1 <=2 を condition1 と condition2 として使用します)。

with 
  v1 as (select count(column4) as c4_cnt from table1),
  v_cond1 as (select column1, column2, column3 from table1 where column1 > 2),
  v_cond2 as (select column1, column2, column3 from table1 where column1 <= 2)
select 
  v1.*, 
  coalesce(v_cond1.column1, v_cond2.column1) as column1,  
  coalesce(v_cond1.column2, v_cond2.column2) as column2,
  coalesce(v_cond1.column3, v_cond2.column3) as column3
from v1
left join v_cond1 on v1.c4_cnt = 0
left join v_cond2 on v1.c4_cnt != 0  
于 2013-07-03T10:56:33.583 に答える