Skip to content
Advertisement

Blaze-Persistence GROUP BY in LEFT JOIN SUBQUERY with COALESCE in root query

I need statistics about alert tagged by codes. Alerts are visible for users with group based restrictions. In case when some tag code is only in alerts which are invisible for user then 0 should be displayed in statisitics.

Tables structure:

I’m using Blaze-Persistence and try to make GROUP BY in LEFT JOIN SUBQUERY with COALESCE in root query. Reason of using Blaze-Persistence has support for subquery in left join.

This is my code:

During execution I expect to get this query:

but I get this one:

Results for both queries are different.

  1. Why this union apper?
  2. Does it’s possible to get query without union in Blaze persistence API?

Advertisement

Answer

You are missing your on-condition in the query builder:

The union is just used for naming the columns but it shouldn’t be an issue since it doesn’t produce a result and the optimizer should be able to optimize it away. Do you see problems with that? The main reason for doing this is, that naming the items of the subquery would otherwise require a parsing and adapting of the SQL which is avoided as much as possible.

Databases like e.g. PostgreSQL support the use of aliases after the table alias. Apparently you are using a database that does not support this like e.g. Oracle or MySQL?

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