I have 2 tables with identical columns names. I have model numbers in each that I want return and if they are in both tables I want them to sum. I tried to use union but it just added the same model number twice. Any ideas how to achieve this below:
Table 1
---------------
Model quantity
BC123 20
BC124 10
Table 2
---------------
Model quantity
BC123 10
BC125 20
Desired result
--------------
BC123 30
BC124 10
BC125 20
Union gives me
--------------
BC123 20
BC123 10
BC124 10
BC125 20
I tried Union:
Table 1
---------------
Model quantity
BC123 20
BC124 10
Table 2
---------------
Model quantity
BC123 10
BC125 20
Desired result
--------------
BC123 30
BC124 10
BC125 20
Union gives me
--------------
BC123 20
BC123 10
BC124 10
BC125 20
I tried Union:
Code:
SELECT
Sa_product as model,
Sa_desc as description,
cname.cn_hclass as itemclass,
cname.cn_hchar as charname,
sum(Sa_qty) as quantity
FROM SANAL
JOIN CNAME ON SANAL.Sa_product = CNAME.CN_REF
WHERE
cn_hclass ='BC' AND
sa_trdate >= { d '2011-07-01'} AND
sa_trdate <= { d '2011-08-10'} AND
left(sa_product,3) != 'ZZZ' AND
left(sa_product,3) != 'DIS'
UNION All
SELECT
Sa_product as model,
Sa_desc as description,
cname.cn_hclass as itemclass,
cname.cn_hchar as charname,
sum(Sa_qty) as quantity
FROM HSANAL
JOIN CNAME ON HSANAL.Sa_product = CNAME.CN_REF
WHERE
cn_hclass ='BC' AND
sa_trdate >= { d '2011-07-01'} AND
sa_trdate <= { d '2011-08-10'} AND
left(sa_product,3) != 'ZZZ' AND
left(sa_product,3) != 'DIS'
GROUP BY model
HAVING sum(Sa_qty) != 0
ORDER by quantity DESC