Skip to content
Advertisement

EXISTS not working with subquery in WITH clause

I ran into a query where the EXISTS clause is not working properly. The query returns results even for items where no matching record exist, seemingly ignoring the EXISTS completely. It used to work fine and I think the troubles started after upgrading from Oracle 12.1 to 12.2.

Below is the complete query (only changed table & column names to make them a little more readable, but I left all the logic in case it’s related to that):

After playing around with some test data, I think the (partially?) responsible line for the failure is the subquery AND ReferenceDate >= ( SELECT StartDate FROM StartDateTable WHERE ItemId = :itemId ) in the second WITH.

I have found that any of the following edits result in the EXISTS working as expected again:

  • JOIN ResultTable with SecondDateFilter (on ReferenceDate)
  • Put ( SELECT ReferenceDate FROM SecondDateFilter WHERE SecondDateFilter.ReferenceDate = ResultTable.ReferenceDate ) in the SELECT ... FROM ResultTable
  • Comment out StartDateTable subquery (no longer filtering on that table, but otherwise it works again)
  • Move StartDateTable subquery to the first WITH

The last solution actually fixes my problem with this query (technically not the same, but the underlying business logic checks out that the result will always be the same), but I was wondering if there was a general problem with the EXISTS clause (potentially only in Oracle 12.2?) that I should be aware of. I have a lot more queries that make use of it.

Below is a test script that duplicates the error. The query below returns 2 rows as expected, but removing the commented line gives 5 rows.

Advertisement

Answer

As per Jonathan’s comments over on Twitter, the suggested workaround is to use the unnest hint in the outer exists subquery, as the issue is due to a bug (potentially Bug 28319114).

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