Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

join two tables without common key

nielsvisser98
Discoverer
1,115

Hi,

I have two different CDS views and I would like to join them. However, they have no common key on which I would like to join them. I would simply like to have the two tables in one CDS view, without changing any of the data or fields.

To visualuze my struggle: I have table A and B, and would like to get the code to come to the OUTCOME table:

Anyone who can help?

Thanks

1 ACCEPTED SOLUTION

MateuszAdamus
Active Contributor
573

Hello nielsvisser98

SELECT a1 AS a1, a2 AS a2, a3 AS a3, '' AS b1, '' AS b2, '' AS b3
  FROM table_A
UNION
SELECT '' AS a1, '' AS a2, '' AS a3, b1 AS b1, b2 AS b2, b3 AS b3
  FROM table_B

Kind regards,

Mateusz
3 REPLIES 3

MateuszAdamus
Active Contributor
574

Hello nielsvisser98

SELECT a1 AS a1, a2 AS a2, a3 AS a3, '' AS b1, '' AS b2, '' AS b3
  FROM table_A
UNION
SELECT '' AS a1, '' AS a2, '' AS a3, b1 AS b1, b2 AS b2, b3 AS b3
  FROM table_B

Kind regards,

Mateusz

joltdx
Active Contributor
573

Hi!

I believe you should be able to accomplish this with UNION. Here's the documentation.

The sets you UNION need to have the same structure, but I suppose it would be possible to "fake" that by selecting empty fields as B1, B2 and B3 for the table A select and vice versa.

joltdx
Active Contributor
573

Hah, mateuszadamus beat me to it. I'll leave my answer here with the link to the documentation... 🙂