John Bustos
John Bustos

Reputation: 19574

Oracle Join - Return non-joined column

I'm having the toughest time figuring out the join syntax for this query.

Suppose I have the following result from an Oracle SQL query:

Result1:

Id1:    Id2:    Char1:    Char2:    ...   CharN:
123a    ABC     c1a       c2a       ...   cNa
123b    DEF     c1b       c2b       ...   cNb

And, in the same database, I have a "mapping table" based upon Id2 simply with the following:

Mapping Table:

Id1:    Id2:
123a    ABC
234a    ABC
345a    ABC
456a    ABC
123b    DEF
234b    DEF

And, now what I would like to do is duplicate my results for each Id1 in the mapping table where the Id2's match - giving me the final result:

DESIRED RESULT:

Id1:    Id2:    Char1:    Char2:    ...   CharN:
123a    ABC     c1a       c2a       ...   cNa
234a    ABC     c1a       c2a       ...   cNa
345a    ABC     c1a       c2a       ...   cNa
456a    ABC     c1a       c2a       ...   cNa
123b    DEF     c1b       c2b       ...   cNb
234b    DEF     c1b       c2b       ...   cNb

In other words, repeat the results with Id1 changed for each record from the mapping table where Id2 matches.

I'm trying to do it with some kind of an Outer Join, but I can't get the syntax correct at all!

Thanks for your help!!

Upvotes: 0

Views: 59

Answers (1)

sgeddes
sgeddes

Reputation: 62851

If I'm understanding your question correctly, you don't want to join on Id1, but rather on Id2. Try this:

SELECT M.Id1, M.Id2, R.Char1, R.Char2
FROM Mapping M 
   JOIN Result1 R ON M.Id2 = R.Id2

This could possibly return duplicate records if you have duplicate rows in the Result1 table with Id2, in which case, you'd have to tell us which record to match it to or use the duplicates.

And here is the SQL Fiddle.

Good luck.

Upvotes: 1

Related Questions