Left Join problem

I have 2 tables in a Firebird 1.5 database such as:
TABLE1
F1     F2      F3
============
1       AA      00
2       BB      10
3       CC      15
4       DD      20

TABLE2
F1     F4    
======
1       aaa
1       bbb
2       aaa
1       ccc
3       ddd
3       aaa
2       eee

So to join using fields F1

I want to get this output using an SQL command:
F1     F2      F3     FIRST of F4
==================
1       AA      00      aaa
2       BB      10      aaa
3       CC      15      ddd
4       DD      20      NULL

What is the SQL command for this output?
Thanks
muratyamanAsked:
Who is Participating?
 
ezraaConnect With a Mentor Commented:
You can add another field to table 2: a running index so your table will now look like:

TABLE2
ID    F1     F4    
==========
1      1       aaa
2      1       bbb
3      2       aaa
4      1       ccc
5      3       ddd
6      3       aaa
7      2       eee

select F1, F2, F3,
   (Select first 1 F4 from Table2 where table2.F1 = table1.F1 order by ID),
   (Select first 1 F5 from Table2 where table2.F1 = table1.F1 order by ID)
from Table1


If you are just worried about getting a pair this should work (though I don't have firebird on my computer, so I haven't checked this):
select F1, F2, F3,
   (Select first 1 F4 from Table2 where table2.F1 = table1.F1),
   (Select first 1 F5 from Table2 where table2.F1 = table1.F1 and F4 = (Select first 1 F4 from Table2 where table2.F1 = table1.F1))
from Table1
0
 
Nick UpsonConnect With a Mentor Principal Operations EngineerCommented:
as it stands I don'tthink you can, when you select from a table the ONLY way to be sure of the sequence of
data you get is to order by a column and table 2 has no column that gives the order you want.
It should be easy to add one and then you can set it to the sequence you want.
if you had wanted the numerically highest value in the First_of_f4 column the ordering could have been on
that but the results would include "3       CC      15      aaa"
please let us know if you can add such a column or in some other way derive the ordering
0
 
ezraaCommented:
select F1, F2, F3, (Select first 1 F4 from Table2 where table2.F1 = table1.F1) from Table1
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Nick UpsonPrincipal Operations EngineerCommented:
(Select first 1 F4 from Table2 where table2.F1 = table1.F1) - but you can't ensure the required ordering
to just pick the first
0
 
muratyamanAuthor Commented:
Thanks

nickupson, the order is not important at the moment; just (any) first value is enough.
yes we can add another column to sort and get record more appropriately.

ezraa, you have the right query.

but actual problem is a bit harder :)

there is another field F5 as well. so when we get the first record we need to get the couple.

TABLE2
F1     F4      F5
===========
1       aaa    x
1       bbb    y
2       aaa    w
1       ccc     z
3       ddd    x
3       aaa    u
2       eee    v

so I used a similar query to ezraa's like:

select F1, F2, F3,
   (Select first 1 F4 from Table2 where table2.F1 = table1.F1),
   (Select first 1 F5 from Table2 where table2.F1 = table1.F1)
from Table1

to get

F1     F2      F3     FIRST of F4   FIRST of F5
=============================
1       AA      00      aaa                    x
2       BB      10      aaa                    w
3       CC      15      ddd                    x
4       DD      20      NULL                 NULL

so according to this example,
can I make sure that Firebird ALWAYS gives me the right couple (F4, F5)?
(maybe, this is related to Firebird's interpretation of FIRST keyword.)
e.g. Talking for first row, it does NOT give y instead of x, right?

Thanks
0
 
muratyamanAuthor Commented:
sorry I wanted to increase teh points as well, it didn't.. i'm trying again
0
 
Nick UpsonPrincipal Operations EngineerCommented:
muratyaman,
> e.g. Talking for first row, it does NOT give y instead of x, right?

it could give x or y, you cannot rely upon the order that

"Select first 1 F5 from Table2 where table2.F1 = table1.F1"

will get the data and hence you don't know which row will be first
0
 
ezraaCommented:
Or you can write a Store procedure:

set term ^ ;
CREATE PROCEDURE table12
RETURNS
(
 F1 integer,
 F2 varchar(10),
 F3 varchar(10),
 F4 varchar(10),
 F5 varchar(10)
)
AS
  begin
  for select F1, F2, F3 from table1 into :F1, :F2, :F3 do
  begin
    select F4, F5 from table2 where F1 = :F1 into :F4, :F5;
    suspend;
  end
  end
 ^
set term ; ^

select * from table12;

 (once again I don't have firebird on my computer so there are probaly mistakes...)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.