简体   繁体   中英

SQL Union/Merge two rows DB2

In my current result are following lines:

item|val1|val2
9999|0   |50
9999|50  |0

How can i merge them into:

9999|50  |50

I cant do subquery based on item, the results of each query are providing a list of items

SQL:############################################
select item, sum(val1), 0 as dummy from itemdata
union
select item, 0 as dummy, sum(val2) from itemdata2

Simply use:

SELECT item, sum(val1), sum(val2)
FROM table
GROUP BY item;

If you are using multiple tables:

SELECT item, sum(val1), sum(val2)
FROM (SELECT item, val1, 0 AS val2 FROM itemdata
      UNION ALL
      SELECT item, 0, val2 FROM itemdata2) sub
GROUP BY item;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM