Skip to content
Advertisement

How to return several columns for subquery added to select?

I have a query like this:

I don’t like code duplication for counts but if I do smth like

in subquery postgres complains that subquery must return single column.

How to fix it ?

Advertisement

Answer

You can use a lateral join:

You can also do this with aggregation:

This could return NULL values, which you can convert to 0 if desired using coalesce().

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