We help IT Professionals succeed at work.

Way to Join only the first record

Jamil Muammar
on
100 Views
Last Modified: 2018-07-04
Dear Experts,

 How Do I in Oracle SQL make Left join between two tables which take only the first record in the right join?

For Example
Table 1:
A  100
B  200
C  300

Table:2
A 900
A 300

The Result will be
A 100 900
B 200 null
C 300 null

Thanks
Comment
Watch Question

ste5anSenior Developer
CERTIFIED EXPERT

Commented:
Further constraints?

Otherwise just use a sub-query as source for the left joined table, which return a single row.
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
What determines the "first" row - maximum value maybe?
I agree you should set up a subquery as table source, which delivers only those rows you want to have joined. As soon as you are able to write up that query, the remainder is easy.
awking00Information Technology Specialist
CERTIFIED EXPERT

Commented:
This can work for your example, but be careful since any inserts and/or deletes can radically change the results
select t1.col1 as t1_value, t2.col1 as t2_value from
(select col1, rownum rn from table1) t1
left join
(select col1, rownum rn from table2
 where rownum = 1) t2
on t1.rn = t2.rn;
Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
awking00, that can't work, because the row numbers of each table have no connection to each other, and rownum is the row number of the select, not the table.
Information Technology Specialist
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions