sensen ol
sensen ol

Reputation: 33

DB2 Combine sql results

I want to combine two result sets

ResultSetA

ID ID_EXT List_A
1 1_ 1,2,3,4,5
2 1_ 20,30,50

ResultSetB

ID ID_EXT List_B
1 1_ B01,B0
2 1_ B20,B30,B50

ResultSet should be

ID ID_EXT List_A List_B
1 1_ 1,2,3,4,5 B01,B0
2 1_ 20,30,50 B20,B30,B50

Upvotes: 0

Views: 39

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522506

Assuming each result set can be obtained by querying some CTE, then you only need a join here:

SELECT
    a.ID,
    a.ID_EXT,
    a.List_A,
    b.List_B
FROM cteA a
INNER JOIN cteB b
    ON b.ID = a.ID AND
       b.ID_EXT = a.ID_EXT
ORDER BY
    a.ID,
    a.ID_EXT;

The above logic might have to change, depending on whether or not a record in either CTE result set might not match to the other one. In that case, a left join or perhaps a full outer join might be more appropriate.

Upvotes: 2

Related Questions