Hello all,
I am attempting to average out results of a query I created. Right now, if any of the fields contain a zero, the resulting average field comes up NULL. I need to somehow make it so the function ignores the zeros and only calculates the actual amounts. Any ideas? Here is the portion of the code that is performing the average:
SELECT
A.COMPANY, A.PAYGROUP, A.PAYCHECK_NBR, A.OFF_CYCLE, A.EMPLID, A.EMPL_RCD, A.NAME,
A.DEPTID, A.SSN, A.PAY_END_DT,
COALESCE(A.NET_PAY,0) AS WEEK_1,
CASE WHEN B.PAY_END_DT IS NULL THEN '2007-10-28' ELSE B.PAY_END_DT END AS PAY_END_DT,
COALESCE(B.NET_PAY,0) AS WEEK_2,
CASE WHEN C.PAY_END_DT IS NULL THEN '2007-10-21' ELSE C.PAY_END_DT END AS PAY_END_DT,
COALESCE(C.NET_PAY,0) AS WEEK_3,
CASE WHEN D.PAY_END_DT IS NULL THEN '2007-10-14' ELSE D.PAY_END_DT END AS PAY_END_DT,
COALESCE(D.NET_PAY,0) AS WEEK_4,
(SUM(A.NET_PAY) + SUM(B.NET_PAY) + SUM(C.NET_PAY) + SUM(D.NET_PAY))/4 AS AVERAGE
FROM NET_PAY A LEFT OUTER JOIN NET_TMP_ONE B
ON A.EMPLID = B.EMPLID
LEFT OUTER JOIN NET_TMP_TWO C
ON A.EMPLID = C.EMPLID
LEFT OUTER JOIN NET_TMP_THREE D
ON A.EMPLID = D.EMPLID
GROUP BY A.COMPANY, A.PAYGROUP, A.PAYCHECK_NBR, A.OFF_CYCLE, A.EMPLID, A.EMPL_RCD, A.NAME,
A.DEPTID, A.SSN, A.PAY_END_DT,
COALESCE(A.NET_PAY,0),
CASE WHEN B.PAY_END_DT IS NULL THEN '2007-10-28' ELSE B.PAY_END_DT END,
COALESCE(B.NET_PAY,0),
CASE WHEN C.PAY_END_DT IS NULL THEN '2007-10-21' ELSE C.PAY_END_DT END,
COALESCE(C.NET_PAY,0),
CASE WHEN D.PAY_END_DT IS NULL THEN '2007-10-14' ELSE D.PAY_END_DT END,
COALESCE(D.NET_PAY,0)
I'm not sure if the CASE or COALESCE functions are causing the problem or if there is simply a piece of code I am missing so it will ignore any 0 values and still perform the average on fields that have actual amounts other than 0.
Thank you all for your help!
I am attempting to average out results of a query I created. Right now, if any of the fields contain a zero, the resulting average field comes up NULL. I need to somehow make it so the function ignores the zeros and only calculates the actual amounts. Any ideas? Here is the portion of the code that is performing the average:
SELECT
A.COMPANY, A.PAYGROUP, A.PAYCHECK_NBR, A.OFF_CYCLE, A.EMPLID, A.EMPL_RCD, A.NAME,
A.DEPTID, A.SSN, A.PAY_END_DT,
COALESCE(A.NET_PAY,0) AS WEEK_1,
CASE WHEN B.PAY_END_DT IS NULL THEN '2007-10-28' ELSE B.PAY_END_DT END AS PAY_END_DT,
COALESCE(B.NET_PAY,0) AS WEEK_2,
CASE WHEN C.PAY_END_DT IS NULL THEN '2007-10-21' ELSE C.PAY_END_DT END AS PAY_END_DT,
COALESCE(C.NET_PAY,0) AS WEEK_3,
CASE WHEN D.PAY_END_DT IS NULL THEN '2007-10-14' ELSE D.PAY_END_DT END AS PAY_END_DT,
COALESCE(D.NET_PAY,0) AS WEEK_4,
(SUM(A.NET_PAY) + SUM(B.NET_PAY) + SUM(C.NET_PAY) + SUM(D.NET_PAY))/4 AS AVERAGE
FROM NET_PAY A LEFT OUTER JOIN NET_TMP_ONE B
ON A.EMPLID = B.EMPLID
LEFT OUTER JOIN NET_TMP_TWO C
ON A.EMPLID = C.EMPLID
LEFT OUTER JOIN NET_TMP_THREE D
ON A.EMPLID = D.EMPLID
GROUP BY A.COMPANY, A.PAYGROUP, A.PAYCHECK_NBR, A.OFF_CYCLE, A.EMPLID, A.EMPL_RCD, A.NAME,
A.DEPTID, A.SSN, A.PAY_END_DT,
COALESCE(A.NET_PAY,0),
CASE WHEN B.PAY_END_DT IS NULL THEN '2007-10-28' ELSE B.PAY_END_DT END,
COALESCE(B.NET_PAY,0),
CASE WHEN C.PAY_END_DT IS NULL THEN '2007-10-21' ELSE C.PAY_END_DT END,
COALESCE(C.NET_PAY,0),
CASE WHEN D.PAY_END_DT IS NULL THEN '2007-10-14' ELSE D.PAY_END_DT END,
COALESCE(D.NET_PAY,0)
I'm not sure if the CASE or COALESCE functions are causing the problem or if there is simply a piece of code I am missing so it will ignore any 0 values and still perform the average on fields that have actual amounts other than 0.
Thank you all for your help!