Sum with NULL values - The lack of logic is blowing my mind...
607743Nov 26 2007 — edited Nov 26 2007I have this query...
WITH
STEP_1 AS
(
SELECT
WO.ACCOUNT_NUMBER,
WO.WORK_ORDER_NUMBER,
CASE WHEN WOD.SERVICE_CODE = '101' THEN WOD.TO_QUANTITY ELSE NULL END LIM,
CASE WHEN WOD.SERVICE_CODE = '111' THEN WOD.TO_QUANTITY ELSE NULL END EXP,
CASE WHEN WOD.SERVICE_CODE = '30001' THEN WOD.TO_QUANTITY ELSE NULL END HSI,
CASE WHEN WOD.SERVICE_CODE IN ('LINE FL', 'ULTS FL') THEN WOD.TO_QUANTITY ELSE NULL END PHONE
FROM
KAN_WORK_ORDER_MASTER WO INNER JOIN KAN_WORK_ORDER_DETAIL WOD ON WO.WORK_ORDER_NUMBER = WOD.WORK_ORDER_NUMBER
WHERE
WO.WO_TYPE IN ('DI', 'DW', 'IN', 'SR', 'UP')
AND WOD.SERVICE_CODE IN ('101', '111', '30001', 'LINE FL', 'ULTS FL')
AND POOL = 'A')
SELECT
ACCOUNT_NUMBER,
WORK_ORDER_NUMBER,
SUM(LIM) LIM,
SUM(EXP) EXP,
SUM(HSI) HSI,
SUM(PHONE) PHONE
FROM
STEP_1
GROUP BY
ACCOUNT_NUMBER,
WORK_ORDER_NUMBER
ORDER BY
ACCOUNT_NUMBER
Sometimes - and more often than not - WOD.TO_QUANTITY is a NULL value - so in the second statement I am usually summing all NULL values...
The output looks like the following...
35808 26133083 1 1
92604 26073490 1
108701 26109659 1 1
126604 26117616 0 0
127603 26030369 0
146808 26127574 1 1 1 1
The blank columns are presumably NULL - however, when I take the query above and turn it into a temporary table - and query the table with the following query..
SELECT * FROM MY_WO_DETAIL WHERE LIM = 0
and the query
SELECT * FROM MY_WO_DETAIL WHERE LIM != 0
There are records that do not show up in either of those statements - and logic would tell me that they would be in one or the other...
So, if I have 5 fields that are null and I sum them, how do I find those blank values when doing a query? I have tried LIM = NULL, LIM = '', LIM < 0, and nothing will identify these records...
Sorry if this is a bit confusing - I understand that you don't have access to this database or understand what it is for - so any help would be appreciated.
EDIT - I cannot do ELSE 0 on the CASE statement - 0 is different than NULL and means two completely different things.
Thanks!
Brett
Message was edited by:
Brettus