all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

Need help with query


Need help with query Sandy
9/25/2007 5:34:00 PM
sql server programming:
Hello -

I have a table as follows:

tblLoans
LoanID PK
DateEntered
FileNumber
LoanType
etc.

And another as follows:
tblBorrowers
BorrowerID PK
BorrowerLast
BorrowerFirst
etc.
LoanID FK

These tables have a one to many relationship with tblLoans on the one side
and tblBorrowers on the many side.

When I join these tables, I need to just return one each of the Borrowers,
regardless of whether or not there is one borrower or five borrowers for each
LoanID. If there's more than one borrower, I only want the first one
returned.

How can I do this?

Any help will be greatly appreciated!

--
Re: Need help with query Alex Kuznetsov
9/25/2007 6:02:35 PM
[quoted text, click to view]

select l.*, b.*
from loans l join borrowers b on l.loadID = b.loanID
and not exists(select 1 from borrowers b1 where b.loandID = b.loanID
and b.borrowerID < b1.borrowerID)
Re: Need help with query Jack Vamvas
9/26/2007 12:00:00 AM
One way is :
SELECT t1.borrowerID,<cols> FROM tblBorrowers as t1
WHERE t1.borrowerId IN (SELECT TOP 1 t2.BorrowerId FROM tblBorrowers as t2
WHERE t1.LoanID = t2.LoanId ORDER BY <pick a criteria> )

--

Jack Vamvas
___________________________________
Search IT jobs from multiple sources- http://www.ITjobfeed.com/SQL




[quoted text, click to view]

AddThis Social Bookmark Button