In SQL, aggregate functions sum(x), avg(x), max(x), min(x) will return NULL when x is empty or contains only NULL values.
In KDB, sum and avg return zero, while max and min return (+ or -) infinity.
To get the desired behavior of returning null I have come up with 2 options:
(A) Remove the nulls, and check if the result is empty before aggregating as usual
nsum:{[x] x: x where not null x; $[0=count x; 0N; sum x]}
(B) Create a new aggregate which checks each item for null before accumulating
Note that we still have to check for an empty list.
nsum:{[z] $[0=count z; 0N; {[x;y] $[null x; y; null y; x; x+y]}/ z ]}
Is there a better (faster and/or more convenient) way than either A or B?
Will method (B) perform any better than method (A) ?
Method (A) has the advantage of working for any aggregate function. Method (B) does not work for "average", because there is no pairwise function which will produce a running average (needs to know how many items were already applied).
A related question: is there a faster way to check for an empty list than 0=count x
, for example 0= type first x
?
THANKS!