Reputation: 1526
Is there any way to create query for such request without 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
Upvotes: 0
Views: 321
Reputation: 30765
You could use two LEFT JOINS and COALESCE (here, I use column1 > 2 / column1 <=2 as condition1 and 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
Upvotes: 0
Reputation: 2886
select column1, column2, column3
from table
where (condition1 and (select count(column) from table) = 0)
or (condition2 and (select count(column) from table) != 0)
Upvotes: 1
Reputation: 4142
SQL = SELECT QUERY;
if(Column != 0)
{
SELECT QUERY;
}
if(Column2 != 0)
{
SELECT QUERY;
}
It should work when you don't add an else statement.
Upvotes: 0