How can I get the AVG
of a column ignoring NULL
and zero values?
I have three columns to get their average, I try to use the following script:
SELECT distinct
AVG(cast(ISNULL(a.SecurityW,0) as bigint)) as Average1
,AVG(cast(ISNULL(a.TransferW,0) as bigint)) as Average2
,AVG(cast(ISNULL(a.StaffW,0) as bigint)) as Average3
FROM Table1 a, Table2 b
WHERE a.SecurityW <> 0 AND a.SecurityW IS NOT NULL
AND a.TransferW<> 0 AND a.TransferWIS NOT NULL
AND a.StaffW<> 0 AND a.StaffWIS NOT NULL
AND MONTH(a.ActualTime) = 4
AND YEAR(a.ActualTime) = 2013
I don't get any results, however the three columns have values including NULL and zeros!
Is there anyway to exclude null values before getting the average?
example: AVERAGE(NOTNULL(SecurityW))
AND
s mean that you'll only get rows which have non-NULL values in all 3 columns. Also, yourNOT NULL
checks aren't required, sinceColumn<>0
evaluates asUNKNOWN
ifColumn
isNULL
. – Dynamometer