Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to join a table column values with another table column names

Orcl ApexJan 11 2024

Good morning,

We are on database 19.20 EE and have data in two of the tables as below.

DROP TABLE t1;
CREATE TABLE t1 (data VARCHAR2 (10)) ;
INSERT INTO t1 (data) VALUES ('A') ;
INSERT INTO t1 (data) VALUES ('B') ;
INSERT INTO t1 (data) VALUES ('C') ;

Table T1 has values like A, B, and C, but its column count is always fewer than that of Table T2, as it is not fixed. The values in Table T1 might contain just A, A and B, or A and B and C at different times. Therefore, the answer shouldn't change depending on the number of rows in Table T1. It's also important to notice that they correspond to the table t2 columns' names.

DROP TABLE t2;
CREATE TABLE t2 (a VARCHAR2 (10), b VARCHAR2 (10), c VARCHAR2 (10), d VARCHAR2 (10), e VARCHAR2 (10)) ;
INSERT INTO t2 (a, b, c, d, e) VALUES ('X', ‘Y’, 'Z', ‘G’, ‘?’) ;

Columns A, B, C, D, and E in Table T2 contain some data that isn't really relevant to my question and only column names are as they must join with t1 values.

Requirement:

Tables T1 and T2 must be joined so that the values in Table T1 join the columns of Table T2. In the same manner that table t2 value B must join with table t2 column B and return value Y, and so on, table t1 value A must join with table t2 column A, for example, and return the value x saved. Table T1 may have just A and B, in which case those values must join with table T2's A and B columns alone in order to return data stored in those columns. However, as of right now, table T1 has three values: A, B, and C. These values must then be compared to Table T2's columns A, B, and C in order to return data stored in those columns.

Desired result:

X Y Z

This post has been answered by BluShadow on Jan 11 2024
Jump to Answer
Comments
Post Details
Added on Jan 11 2024
10 comments
2,319 views