473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

JOINS

Hi All,

This is a simple problem I expect, but I cant seem to crack it.

I have to tables I wish to join A & B. I wish to join them where A.a =
B.a. There are multiple entries for B.a, and I wish to join to the
rows with the lowest B.b.

SELECT
A.a
MIN(B.b)
FROM
A
LEFT JOIN B
ON (A.a = B.a)
GROUP BY
A.a

That works fine but I also want to extract the other columns from B on
the row that is matched

i.e.
A B
..a .a .b .c
1 1 5 8
2 1 8 2
2 7 9
2 1 7

So I want to obtain

A.a B.a B.b B.c
1 1 5 8
2 2 1 7
An alternative is to use the WHERE instead of the JOIN

SELECT
A.a
B.b
B.c
MIN(B.b)
FROM
A
,B
WHERE
A.a = B.a
AND B.c = (SELECT MIN(B.c) FROM B WHERE A.a = B.a)
But in some instance there is no match at all in B, and 'where' will
lost the A information. Additionally I have a number of row from B I
need to return;B.d, B.e etc.

Any suggestions

Thanks

Terry
Nov 12 '05 #1
1 1244
Terry wrote:
Hi All,

This is a simple problem I expect, but I cant seem to crack it.

I have to tables I wish to join A & B. I wish to join them where A.a =
B.a. There are multiple entries for B.a, and I wish to join to the
rows with the lowest B.b.
SELECT ...
FROM A JOIN ( SELECT ...
FROM B
WHERE b <= ALL ( SELECT b
FROM B ) ) AS B ON
( A.a = B.a )
But in some instance there is no match at all in B, and 'where' will
lost the A information. Additionally I have a number of row from B I
need to return;B.d, B.e etc.


Then you need an OUTER JOIN - either LEFT OUTER JOIN or RIGHT OUTER JOIN (I
always mix up in which table the rows are preserved if there are no
matching rows in the other table).

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jgalzic | last post by:
Hi, I'm having trouble doing joins correctly on two tables. I've read up a lot about the different types of joins and tried lots of variations on inner, outer, and left joins with no avail....
3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
4
by: Sri | last post by:
I am writing a download process in which i have a condition where i need to join four tables. Each table have lot of data say around 300000 recs. my question is when i am doing the joins on...
1
by: Prem | last post by:
Hi All Database Gurus, I am trying to write code which will produce all the possible valid queries, given tables and join information for tables. Right now i am just trying to construct all the...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
7
by: Steve | last post by:
I have a SQL query I'm invoking via VB6 & ADO 2.8, that requires three "Left Outer Joins" in order to return every transaction for a specific set of criteria. Using three "Left Outer Joins"...
2
by: narendra vuradi | last post by:
Hi I have a requirement where in i haev to convert the SQL from Oracle to the one which will run on the SQL server. in the Oracle Query i am doing multiple joins, between some 13 tables. and...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
36
by: TC | last post by:
I've used Access for many years. Several times, I've encountered a bug which I refer to as the "Vanishing Joins" bug. When it happens, joins vanish randomly from queries. More specifically, all...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.