Skip to content
Advertisement

How to make pivot query that returns no rows return null instead. (Oracle SQL)

I have a query that is structured like this:

For the case of it returning no values, I want it to return null-values instead of a empty row.

How would I achieve this? I was not able to make commonly suggested solutions work, because of the pivot.

E:

The result I get:

No row returned:

enter image description here

The result I want:

One row returned with null values

enter image description here

Advertisement

Answer

The problem is not with the PIVOT it is with the sub-query before the PIVOT and your question can be reduced to:

How can I get the query (renaming your identifiers to have legal values):

to always return at least one row?

If you have the test data:

Then there is no row that will match the filter conditions in your WHERE clause and there will be zero rows to PIVOT so the query will always return zero rows. In this case then you can use UNION ALL to add a row:

Which will output:

 NUM | POSITION
---: | -------:
null |     null

And then wrapped with the pivot:

Outputs:

result1 | result2 | result3
------: | ------: | ------:
   null |    null |    null

db<>fiddle here

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