admin管理员组

文章数量:1278983

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

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
Share Improve this question asked Feb 24 at 14:08 sensen olsensen ol 334 bronze badges 2
  • We need to also see the queries that generated the two original result sets. – Tim Biegeleisen Commented Feb 24 at 14:21
  • It's a quite bit complex to insert the queries here, but it is a with clause. – sensen ol Commented Feb 24 at 14:35
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: DB2 Combine sql resultsStack Overflow