SELECT
(
SELECT COALESCE( nb1, 0 ) + COALESCE( nb2, 0 ) + COALESCE( nb3, 0 ) as ADDITION
)
/
(
SELECT
COALESCE(nb1 /nb1, 0)
+
COALESCE(nb2 /nb2, 0)
+
COALESCE(nb3 /nb3, 0)
AS DIVIDEBY
) AS AVG
FROM YOURTABLE
I think this is the best worse answer. Explanations :
The function COALESCE replace your NULL by another value, here 0.
- The first select (ADDITION), do the sum between each columns and add 0 instead of NULL
The second select (DIVIDEBY) count the number of columns which are not null :
- It add 1 when column are not NULL or equal to 0. (This is why the column is divide by itself.)
- It add 0 when the column is NULL or equal 0.
The top "anonymous" SELECT send each column (nb1, nb2, nb3) to the two sub requests for the selected Table (YOURTABLE)
- The two sub request are divided for each row in order to obtain the average (AVG)
- The AVG request is the column of the top "anonymous" SELECT