Skip to content
Advertisement

Oracle UNION depending an IF conditon

I have to make a SQL query wraping all of theses statements:

First, I have a first SQL query like this:

Then, I have another SQL statement like this:

Finally, based on the first SQL statement, I have to add a row of fake data on the result of the second statement if NbOfEntries > 0. I have to add an UNION to the second SQL query like this:

I thought about using UNION but, I don’t know how to use UNION with a condition. In addition, I do not know how to generate fake data with SELECT in Oracle, I have already looked at how DUAL works but I have not really been able to move forward.

Thank you.

EDIT: Sample data

If the first statement returns 0, I have these rows:

If the first statement returns > 0, I ihave to add one fake row to the result like this:

Advertisement

Answer

You can use EXISTS like this:

There is no need to count the rows of table1.
EXISTS will return TRUE if there is 1 row that meets the condition.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement