If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > is TOP 1 in JOIN possible

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-20-04, 10:17
Charlie01 Charlie01 is offline
Registered User
 
Join Date: Mar 2004
Location: Germany
Posts: 4
is TOP 1 in JOIN possible

Doing a query with two Tables normaly is done by A.IDA=B.IDA
But also A.IDA>B.IDA is possible - but can give more than one join.

I have the following query:

SELECT * FROM TblA
LEFT JOIN TblB ON TblB.Begin > TblA.End

Now I want to get ONLY ONE joined record.

Is there an syntax like:
LEFT JOIN TOP 1 TblB ON TblB.Begin > TblA.End ???
Reply With Quote
  #2 (permalink)  
Old 03-20-04, 21:51
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
SQL Server 2000:

Select top N *
from table
order by column

Oracle 9i:

Select *
from
(select columns from table ORDER BY column)
where rownum = 1;
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 03-21-04, 04:32
Charlie01 Charlie01 is offline
Registered User
 
Join Date: Mar 2004
Location: Germany
Posts: 4
Quote:
Originally posted by r123456
SQL Server 2000:

Select top N *
from table
order by column

Oracle 9i:

Select *
from
(select columns from table ORDER BY column)
where rownum = 1;
Thank you. - But just selecting the top one of a table is not my problem.
I need the top 1 in the JOIN statement, because I want to join one table with onother by joining from the second table only the ONE next elder record.
Reply With Quote
  #4 (permalink)  
Old 03-21-04, 04:46
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Select *
from
tableB tb
LEFT OUTER JOIN
(select top N * from tableA where condition) v1 on
v1.id = tb.id;

This query will join all records of tableB with the first record of the set V1.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #5 (permalink)  
Old 03-21-04, 13:44
Charlie01 Charlie01 is offline
Registered User
 
Join Date: Mar 2004
Location: Germany
Posts: 4
Quote:
Originally posted by r123456
...
(select top N * from tableA where condition) v1 on
v1.id = tb.id;

This query will join all records of tableB with the first record of the set V1.
Sorry - but this doesn't help either
because if top 1 selects a record with another v1.id than tb.id I get no joined records although there IS one (but not on top of the list v1)

Or did I get something wrong...
Reply With Quote
  #6 (permalink)  
Old 03-21-04, 16:45
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
PHP Code:
select *
  
from TblA 
left outer
  join TblB 
    on TblB
.Begin TblA.End
   
and TblB.Begin
     
= ( select max(Begin)
           
from TblB 
          where Begin 
TblA.End 
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-22-04, 07:39
Charlie01 Charlie01 is offline
Registered User
 
Join Date: Mar 2004
Location: Germany
Posts: 4
Thumbs up It works !

Quote:
Originally posted by r937
PHP Code:
select *
  
from TblA 
left outer
  join TblB 
    on TblB
.Begin TblA.End
   
and TblB.Begin
     
= ( select max(Begin)
           
from TblB 
          where Begin 
TblA.End 

THAT WORKS !!!!!!

Thanks a lot !!!!!!!
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

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