Skip to content
Advertisement

How to get the average from multiple columns

I saw this answer how do I select AVG of multiple columns on a single row but I have some rows which is the value is 0 and it’s throwing an error division by zero when I try to use his

ex.

Advertisement

Answer

In order to resolve the task, you can use CASE expression because you cannot divide by zero:

dbfiddle

It is added extra COALESCE because the sum of the divisor can be zero as well

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