sybase sql - how to sum up UNION records

I like to sum up two sql results into one in one shot.

there are two tables, both has same columns but data records are different
I would like to see one line result as total sum value instead of two lines.
select
key,
sum(columnA)
from table A
group by key
UNION
select
key,
sum(columnA)
from table B
group by key

would you advise how this can be made...?
sleepinglukeAsked:
Who is Participating?
 
Jan FranekConnect With a Mentor Commented:
If your version supports derived tables (ASE 12.5.1 and later):

select
key,
sum(columnA)
from (
select
key,
sum(columnA) as columnA
from table A
group by key
UNION
select
key,
sum(columnA)
from table B
group by key ) der_table
group by key
0
 
Jan FranekConnect With a Mentor Commented:
If your version does not support derived tables use #temp table:

select
key,
sum(columnA) as columnA
into #temp
from table A
group by key

insert into #temp
select
key,
sum(columnA)
from table B
group by key

select
key,
sum(columnA)
from #temp
group by key
0
 
sleepinglukeAuthor Commented:
thank you very much it works!
0
All Courses

From novice to tech pro — start learning today.