Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: SQL Question, taking differences between sum of columns
Charles Hooper wrote:
> Anil G wrote:
> SELECT > > NVL(SUM(DECODE(TYPE_CODE,'G1',VAL,0)),0)+NVL(SUM(DECODE(TYPE_CODE,'G2',VAL,0)),0)-NVL(SUM(DECODE(TYPE_CODE,'G3',VAL,0)),0) > MY_SUM > FROM > TBL1;
I doubt the above will work. You should get results similar to this:
MY_SUM
10 23 22 21 -43 0 0 0
Something more on the order of this should work:
SELECT g1.g1_val + g2.g2_val - g3.g3_val FROM (SELECT SUM(val) AS G1_VAL FROM my_table WHERE type='G1') g1,
(SELECT SUM(val) AS G2_VAL FROM my_table WHERE type='G2') g2, (SELECT SUM(val) AS G3_VAL FROM my_table WHERE type='G3') g3;
HTH,
Brian
-- =================================================================== Brian Peasland dba_at_nospam.peasland.net http://www.peasland.net Remove the "nospam." from the email address to email me. "I can give it to you cheap, quick, and good. Now pick two out of the three" - UnknownReceived on Wed Jul 12 2006 - 07:57:55 CDT