August 3, 2005 at 9:33 am
I am selecting data using a Left Outer Join. I only want the first row that the Join returns.
Is there a way to add to the Join a "Top 1" clause?
My SQL:
Select File_ID, Branch_ID, Other_Columns From Main_Table Left Outer Join Branches On Main_Table.Column = Branches.Column
This will return 2 or more rows with the same File_ID. I only want File_ID once. (The Branch_ID is not as important, but I would like to get the first one.)
DISTINCT will not work here, since File_ID and each Branch_ID will be unique.
Thanks,
Bryan Clauss
August 3, 2005 at 10:08 am
Bryan...as they say...the proof of the pudding is in the eating....
have you tried .....
Select top 1 File_ID,
Branch_ID,
Other_Columns
From Main_Table
Left Outer Join Branches
On Main_Table.Column = Branches.Column
**ASCII stupid question, get a stupid ANSI !!!**
August 3, 2005 at 10:20 am
I have not. I thought that Top 1 will only return 1 row.
This is my dataset that is returned:
File_ID | Branch_ID | Data | Data |
1 | 2 | hghghg | rtrtr |
1 | 3 | sdfsdf | sdfsd |
2 | 9 | hghghg | rtrtr |
3 | 12 | sdfsdf | sdfsd |
3 | 43 | hghghg | rtrtr |
4 | 23 | sdfsdf | sdfsd |
I would like to only get back 1 row per File_ID.
August 3, 2005 at 10:30 am
aah - you need a "group by"...thought you did want only one row...give me some time (unless someone else gets to this first)...will be back!
**ASCII stupid question, get a stupid ANSI !!!**
August 3, 2005 at 10:51 am
This should work. If your "Other_Columns" come from Branches, you will probably need to add those to the sub-select which joined upon.
SELECT [File_ID],
B.Branch_ID,
Other_Columns
FROM Main_Table
LEFT OUTER JOIN( SELECT MAX( Branch_ID) AS Branch_ID, [Column]
FROM Branches GROUP BY [Column]) B ON( Main_Table.[Column] = B.[Column])
I wasn't born stupid - I had to study.
August 3, 2005 at 10:54 am
since I don't see the ddls of the 2 tables I'm just guessing here...but hopefully you can tweak this and get it working....
Select A.File_ID,
A.Branch_ID,
A.Other_Columns
From Main_Table A
Left Outer Join
(select max(Branch_ID) from Branches group by File_ID) B
On A.Branch_ID = B.Branch_ID
**ASCII stupid question, get a stupid ANSI !!!**
August 3, 2005 at 10:56 am
...really must learn to type faster...maybe we should all take lessons from remi...
**ASCII stupid question, get a stupid ANSI !!!**
August 3, 2005 at 10:59 am
There's no lesson to give... think fast and type faster .
August 3, 2005 at 11:08 am
hi remi...definitely can't think as fast as you...but as for "type faster"...you're typing too fast these days...noticed a slip-up this morning in one of your responses that (obviously) came from typing too fast and not (obviously) because you didn't know what you were doing....
**ASCII stupid question, get a stupid ANSI !!!**
August 3, 2005 at 11:23 am
All right, you guys. I feel like a "Newbie" now.
I should have thought of the "sub-Query". Guess I need more caffiene.
Thanks for the help!
August 3, 2005 at 11:31 am
Just remember Bryan - henceforth if tanking up on caffeine doesn't do the trick - SSC/remi (used interchangeably here) always will!
**ASCII stupid question, get a stupid ANSI !!!**
August 3, 2005 at 11:40 am
What post?
August 3, 2005 at 11:44 am
Just remember Bryan - henceforth if tanking up on caffeine doesn't do the trick - SSC/remi (used interchangeably here) always will!
SSC/remi -> I understand Remi (He has posted to this topic...), but SSC? Is that sushila?
August 3, 2005 at 11:52 am
Not enough caffeine yet .
August 3, 2005 at 11:54 am
No Bryan - I would never elevate myself to that altitude (compare myself to remi - that would be arrogance on my part indeed!)
...stands for SQLSERVERCENTRAL.COM!
remi - it was something to do with old quoteid and new quoteid where you left out one part of it - don't remember the forum now!
**ASCII stupid question, get a stupid ANSI !!!**
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply