Solved

Left Join problem

Posted on 2004-10-13
8
827 Views
Last Modified: 2013-12-09
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
0
Comment
Question by:muratyaman
  • 3
  • 3
  • 2
8 Comments
 
LVL 19

Assisted Solution

by:NickUpson
NickUpson earned 100 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:ezraa
Comment Utility
select F1, F2, F3, (Select first 1 F4 from Table2 where table2.F1 = table1.F1) from Table1
0
 
LVL 19

Expert Comment

by:NickUpson
Comment Utility
(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
 

Author Comment

by:muratyaman
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:muratyaman
Comment Utility
sorry I wanted to increase teh points as well, it didn't.. i'm trying again
0
 
LVL 19

Expert Comment

by:NickUpson
Comment Utility
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
 
LVL 9

Accepted Solution

by:
ezraa earned 200 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:ezraa
Comment Utility
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now