0

I have the following table:

|--------|----------------|-------------|
|  url   |   description  |  for_region |
|--------|----------------|------------ |
| url1   |   desc1        |  All        |
| url2   |   desc2        |  All        |
| url2   |   desc3        | Germany     |
|--------|----------------|-------------|

Now I am trying to write the following query without if else statements:

IF EXISTS (SELECT 1 FROM my_table where for_country='Germany') THEN
   select * from my_table where for_country='Germany'
ELSE 
   select * from my_table where for_country='All'
END IF;

What is the best way to rewrite the above query without the use of if-else statements?

5 Answers 5

2

You can add EXISTS into the WHERE clause

select * 
from my_table 
where (EXISTS (select 1 from my_table where for_country='Germany') and for_country='Germany') OR
      (NOT EXISTS (select 1 from my_table where for_country='Germany') and for_country='All')

DBFiddle DEMO

and a probably better solution is to use EXISTS and CROSS JOIN to avoid the duplicate call of the same subquery

select my_table.* 
from my_table 
cross join (
  select exists(select 1 from my_table where for_country='Germany') exst
) t
where (t.exst and for_country='Germany') OR
      (not t.exst and for_country='All')

DBFiddle DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

please can you have a look at this question stackoverflow.com/q/51758821/6633337
0

Try below query:

select m1.name from
   (
    select m1.*, case when m1.for_region='Germany' then 1  else 0  end  as cnt from tableA m1 ) m1
     inner join 
    (
    select max(Cnt) as Cnt from
    (
     select t1.*, case when for_region='Germany' then 1  else 0  end  as Cnt 
      from tableA t1
     ) as t2

     )as n 
     on m1.cnt=n.Cnt

1 Comment

please can have a look at this question stackoverflow.com/q/51758821/6633337
0
select * from my_table 
where 
((SELECT DISTINCT 1 FROM my_table where for_country='Germany') = 1 AND for_country='Germany')
OR for_country='All'

1 Comment

please can you have a look at this question stackoverflow.com/q/51758821/6633337
0

I would use UNION with NOT EXISTS :

SELECT * 
FROM my_table 
WHERE for_country = 'Germany'
UNION ALL
SELECT *
FROM my_table
WHERE for_country = 'All' AND
      NOT EXISTS (SELECT 1 FROM my_table WHERE for_country = 'Germany');

1 Comment

please can you have a look at this question stackoverflow.com/q/51758821/6633337
0

I would write this as:

select t.*
from my_table t.
where (for_country = 'Germany') or
      (not exists (select 1 from my_table where for_country = 'Germany') and
       for_country = 'All'
      );

1 Comment

please can you have a look at this question stackoverflow.com/q/51758821/6633337

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.