?
Solved

Left Join problem

Posted on 2004-10-13
8
Medium Priority
?
892 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 400 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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