DECODE a negative sum
604717Jun 16 2008 — edited Jun 16 2008Hello,
i'm not quite sure if this is the right forum but i wil post my question here.
I want to use a decode statement to get the sum of different topics. My SQL-statement looks like:
select * from
(SELECT month time,
MAX(decode(vcgmk, 'BMW', sum, 0)) BMW,
MAX(decode(vcgmk, 'Porsche', sum, 0)) Porsche,
MAX(decode(vcgmk, 'GM', sum, 0)) GM,
MAX(decode(vcgmk, 'Dodge', sum, 0)) Dodge
FROM
(SELECT TRUNC(date, 'MON') month,
SUM(nwert) as sum,
vcgmk
FROM tab_wak
WHERE(date BETWEEN to_date('01.05.2008 00:00', 'dd.mm.yyyy hh24:mi')
AND to_date('07.05.2008 23:59', 'dd.mm.yyyy hh24:mi'))
GROUP BY TRUNC(date, 'MON'),
vcgmk)
GROUP BY month)
This works fine while the sum is positive. Is the sum negative, the decode-statement returns the default value. And if i switch the default value to null, the sum will be returned correctly but the columns will be null. is there a opportunity to combine both, to get the negativ sum and default value as literal null?
Thanks so far!