Way to Join only the first record

Jamil Muammar
Jamil Muammar used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

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
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

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
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
Commented:
Not only can it work, it does work. However, it's only because the records were inserted in the order of Jamil's example data and not modified in any way.
insert into table1 values(100);
insert into table1 values(200);
insert into table1 values(300);
insert into table2 values(900);
insert into table2 values(300);
SQL> select * from table1;
      COL1
----------
       100
       200
       300
SQL> select * from table2;
      COL1
----------
       900
       300
SQL> select col1, rownum rn from table1;
      COL1         RN
---------- ----------
       100          1
       200          2
       300          3
SQL> select col1, rownum rn from table2;
      COL1         RN
---------- ----------
       900          1
       300          2
SQL> select t1.col1 as t1_value, t2.col1 as t2_value from
  2  (select col1, rownum rn from table1) t1
  3  left join
  4  (select col1, rownum rn from table2
  5   where rownum = 1) t2
  6  on t1.rn = t2.rn;

  T1_VALUE   T2_VALUE
---------- ----------
       100        900
       300
       200

Until we are given a precise and meaningful determination of what constitutes the first row (in both tables), the use of rownum is a readily available option. I could have also used row_number() over (partition by rowid) as rn and it would have produced the same results but, again with deletions and inserts would become skewed.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial