Skip to content
Advertisement

posgresql exclude zero division error results

I am executing a query but for sum cases

this part of the code results into zero which causes zero division error.I am trying to not show result(not select) when

Pease help.

Advertisement

Answer

The simplest way is to remove the else 0:

This returns NULL if nothing matches the condition. Dividing by NULL produces NULL and not an error.

For your query however, I recommend:

nullif() is another way to avoid division by zero. filter is the recommended (and standard) syntax for conditional aggregation.

If you want to filter out the rows that are null, you can include a having clause:

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