Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > join

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-11-04, 19:14
broccoli broccoli is offline
Registered User
 
Join Date: Jun 2004
Posts: 2
join

I try to join 2 tables. First I thought it should be 1 to 1 tables, but then I've found out that the data on the tables is not very clean. In some cases it is possible that there are 2 or none matches. But I need just one of it (it doesn't matter which one).

My select:

select distinct fk_emp, .....
from table1 a
left outer join table2 b on a.key = b.key
where .....

but in some cases, I still get more than 1 result
what is wrong?
thanks
Reply With Quote
  #2 (permalink)  
Old 06-11-04, 19:18
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 9,572
Can you explain what you are trying to do a little better? This sounds like a fundamental logic problem, so at least I need to understand what you are trying to do a bit better so that I can figure out how to help you do it.

-PatP
Reply With Quote
  #3 (permalink)  
Old 06-11-04, 19:53
broccoli broccoli is offline
Registered User
 
Join Date: Jun 2004
Posts: 2
an example:
I have a table with employees (table1) and an other table (table2) with informations like the department, branch .... and a foreign key to a third table where the PersonalID (which is the information I need) is. Now, it is possible that an employee works in more than just one department, so the join with table1 and table2 gives me more than 1 result. How can I join table1 and table2 so I get only one result?

table1
empno 1

table2
empno 1 department FINA Branch ZH FK 3
empno 1 department RISK Branch AG FK 3

table3
persID 3 persno 123456.....
Reply With Quote
  #4 (permalink)  
Old 06-12-04, 08:01
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,554
Quote:
Originally Posted by broccoli
How can I join table1 and table2 so I get only one result?
the first thing you have to do is decide which one of the multiple table2 rows you want

the one with the lowest department name?

the one with the latest date?

the one with the shortest description?

etc.

then you use GROUP BY and MIN(), MAX(), etc.
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On