Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Joins that Filter for only the first entry in a one-to-many

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-12-07 : 08:06:30
David writes "I have a quick problem that would be nice to get a bit of help with.

I want to join empolyees on employee bank accounts but only want the
first record for each employee even though it is a 1-M relationship.
i.e., starting with:
EMP_ID(PK), NAME
ACCT_ID(PK), EMP_ID

resulting in:
EmpID(PK), Name, Acct_ID
(just the first acct_id, no matter how many there are)

It is a trival enough sounding query but SQL doesn't like to throw away perfectly good data."

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-07 : 08:28:20
Refer point 2
http://weblogs.sqlteam.com/mladenp/archive/2005/08/01/7421.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

druer
Constraint Violating Yak Guru

314 Posts

Posted - 2005-12-07 : 09:26:33
select emp.name, emp.address, act.id
from employee emp, (select top 1 id from accounts where accounts.emp_id = emp.emp_id) act
Go to Top of Page
   

- Advertisement -