sum two result sets

  • Is there a keyword to sum these 2 resultsets by rim_no?

    select rim_no, sum(cur_bal) [cur_bal]

    from dp_display

    group by rim_no

    having sum(cur_bal) > 0

    select rim_no, sum(cur_bal) [cur_bal]

    from ex_acct

    where acct_type = 'cdr'

    group by rim_no

    having sum(cur_bal) > 0

  • You did not post any DDL information, nor sample data, so I cannot test it, but here's a shot in the dark...

    This should work:

    SELECT rim_no ,

    SUM([cur_bal]) AS cur_bal

    FROM ( SELECT rim_no ,

    SUM(cur_bal) [cur_bal]

    FROM dp_display

    GROUP BY rim_no

    HAVING SUM(cur_bal) > 0

    UNION ALL

    SELECT rim_no ,

    SUM(cur_bal) [cur_bal]

    FROM ex_acct

    WHERE acct_type = 'cdr'

    GROUP BY rim_no

    HAVING SUM(cur_bal) > 0

    ) AS a

    GROUP BY rim_no

    Cheers,

    J-F

  • Awesome thanks bro!

  • Happy it helped,

    Have a nice day,

    Cheers,

    J-F

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply