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

If select returns 0 then do another select

I have this query that selects data from two different connections

select count (*) from table1
where condition1 = 'answer1'
and time >= sysdate - interval '5' minute

UNION ALL

select count (*) from table2
where condition2 = 'answer2'
AND NOT EXISTS (

select * from table1
where condition = 'answer'
and time >= sysdate - interval '5' minute

);

My problem is that the table2 select doesn’t work at all even though there is no data in table1.

Can anybody help me to get this to work, or knows another way to write the query.

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

The idea is that if select count returns 0 then do another select from different table

Tried googling my way around even lurking here but still didn’t get any answers

>Solution :

If table2 is not connected to table1 someway within the NOT EXISTS clause it will never return data because so long as the query returns something it exists.

If you don’t have a way of linking table2 to table1 that you can use as a join within the not exists you can do something like this

select count (*) from table 1
where condition = answer
and time >= sysdate - interval '5' minute

UNION ALL

select count (*) from table2
where condition2 = answer2
AND (
  select count(*) from table 1
  where condition = answer
  and time >= sysdate - interval '5' minute
) = 0;
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