Skip to content
Advertisement

How to force postgres to return 0 even if there are no rows matching query, using coalesce, group by and join

I’ve been trying hopelessly to get the following SQL statement to return the query results and default to 0 if there are no rows matching the query.

This is the intended result:

Instead I get:

Here is the sql statement:

I know the query works because it returns data when there is data. Just doesn’t default to 0 as intended…

Any help in finding why this is the case would be much appreciated!

Advertisement

Answer

Using your subquery DerivedTable, you could write:

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