×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

SQL LEFT JOIN to return only first matching instance

SQL LEFT JOIN to return only first matching instance

SQL LEFT JOIN to return only first matching instance

(OP)
Hello,
I have been searching for a way to get my expected results but with no luck so far.

Question: my LEFT JOIN operation can result more than one match but I only want the query to return the first matching instance (see sample below). Is that possible?


Table1        
Field1    Field2    
A    D    
B    D    
C    G    
        
Table2        
Field1    Field2    
D    1    
D    2    
G    4    
        
Query        
SELECT * FROM Table 1        
LEFT JOIN Table2        
On Table1.Field2 = Table2.Field1 [+ something to only select first matching instance of the LEFT JOIN]        
        
Expected query results        
Table1.Field1    Table1.Field2    Table2.Field2
A    D    1
B    D    1
C    G    4
 

RE: SQL LEFT JOIN to return only first matching instance

they're called columns, not fields winky smile

CODE

SELECT tA.column1
     , tA.column2 AS tA_column2
     , tB.column2 AS tB_column2
  FROM tA
LEFT OUTER
  JOIN ( SELECT column1
              , MIN(column2) AS min_column2
           FROM tB
         GROUP
             BY column1 ) AS mB
    ON m.column1 = tA.column2
LEFT OUTER
  JOIN tB
    ON tB.column1 = tA.column2
   AND tB.column2 = mB.column2

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon

RE: SQL LEFT JOIN to return only first matching instance

(OP)
R937, your are the pro!

Thank you very much!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close