I have a table which has 32 columns. 2 of these columns are identity columns and the rest are values. I would like to get the average of all these 30 columns, excluding the null ones. If none of the columns were null, that would have been pretty easy as follows select (val0 + val1 + val2 + ...) / 30
Since I would like to exclude the null ones from the sum and divide, I shall rewrite the query similar to the one below (assuming oracle syntax, but same is possible with many other DBs. Just replace NVL with ISNULL for SQL Server, and IFNULL for MySQL):
select (nvl(val0, 0) + nvl(val1, 0) + ...) / "nonZeroColumnCountInThisRow"
The problem is I can not find a way to get the "nonZeroColumnCountInThisRow". Take the following:
"nonZeroColumnCountInThisRow" = ifNullThenZeroElse1(val0) + ifNullThenZeroElse1(val1) ...
The difficulty here is of course in getting "1" for any non-null column. It seems I need a function similar to NVL, but with an else clause. Something that will return 0 if the value is null, but 1 if not, rather than the value itself.
Could you please help.
PS: I feel I must explain some motivation behind this design. Ideally this table would have been organized as the identity columns and one value per row with some identifier for the row itself. This would have made it more normalized and the solution to this problem would have been pretty simple. The reasons for it not to be done like this are throughput, and saving space. This is a huge DB where we insert 10 million values per minute into. Making each of these values one row would mean 10M rows per minute, which is definitely not attainable. Packing 30 of them into a single row reduces the number of rows inserted to something we can do with a single DB, and the overhead data amount (the identity data) much less.