I have a simple looking problem but I don't know how to handle.
I have tow columns which are filled by value or null
.
I have to make an average of these like this:
- if in both are values = (A+B)/2
- if one is null then = A or B.
Is it possible to write it in different way then:
case when a is not null and b is not null then....
etc.
If I use a simple (a+b)/2
I get null
in cases where one of values is null
.
Probably the simplest way is to use
outer apply
withavg()
becauseavg()
ignoresNULL
values:You can also do this with a complicated
case
expression:This works well enough for 2 values; it is feasible for 3. It doesn't generalize well beyond that. Another way to use
case
is a bit more generalizable:But the
apply
method is still simpler.