Just for fun, here is a slightly different solution that leaves the NULL control to the avg() function:
SELECT avg(colValue) from ( SELECT col1 as colValue from tbl UNION ALL SELECT col2 as colValue from tbl UNION ALL SELECT col3 as colValue from tbl )
Values ββfrom co1l, col2 and col3 are placed in one virtual column, and then the database calculates the average value.
You do not need to worry about NULL values ββ- the database function and avg() will do this for you.
Note. . This can be slower than other solutions, as it can cause 3 full table scans to create a virtual table. Check the execution plan.
source share