Solved

Left Join problem

Posted on 2004-10-13
8
888 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 19

Assisted Solution

by:NickUpson
NickUpson earned 100 total points
ID: 12297066
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
ID: 12297956
select F1, F2, F3, (Select first 1 F4 from Table2 where table2.F1 = table1.F1) from Table1
0
 
LVL 19

Expert Comment

by:NickUpson
ID: 12298176
(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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:muratyaman
ID: 12298649
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
 

Author Comment

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

Expert Comment

by:NickUpson
ID: 12300037
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
ID: 12302934
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
ID: 12302988
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

688 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