Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter query in SQL conditionally?

I have a query that shows me the earnings for each shop_id and country as below.

select shop_id,
       country,
       start_date,
       sum(earnings) as earnings
       
from x
where country IN ('DE', 'IT', 'ES')     
group by 1,2,3

However, I want to have only three shops in the country DE and all shops in the rest of the countries.

shop_id IN ('1', '2', '3')

How can I do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It sounds like your criteria could use some nesting, like this:

select shop_id,
       country,
       start_date,
       sum(earnings) as earnings
       
from x
where country IN ('IT', 'ES') 
OR (country = 'DE' AND sop_id IN ('1','2','3'))
group by 1,2,3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading