I have two related tables in my SQL database that I wish to join as follows:
------------------------------------
tblCustomers
ID (pk)
Name
etc.
tblCustomerManagers
ID (pk)
CustomerID (fk)
Manager (this *is* an fk but for the purposes of demonstration is
not)
StartDate (indicates the date upon which the manager took / is taking
control of the company)
------------------------------------
Example entries are:
tblCustomers
1 Microsoft
2 Symantec
3 Borland
tblCustomerManagers
1 1 Barry 01/01/03
2 1 Peter 01/07/03
3 2 Norman 01/02/03
4 3 Terry 01/01/03
5 3 Peter 01/07/05
------------------------------------
What I want to do is extract, in one query, a list of all customers and
their *current* associated manager, so the result set today would be:
Microsoft Peter 01/07/03
Symantec Norman 01/02/03
Borland Terry 01/01/03
Currently I have:
SELECT [Name], [Manager], [StartDate]
FROM tblCustomers
INNER JOIN tblCustomerManagers ON tblCustomerManagers.[CustomerID] =
tblCustomers.[ID]
WHERE [StartDate] <= GETDATE()
ORDER BY [Name], [StartDate] DESC
but this obviously returns multiple entries for customers having managers
prior to today eg:
....
Microsoft Peter 01/07/03
Microsoft Barry 01/01/03
....
I know this is a simple question but I cannot think of a way of doing it
without making the query extremely complicated.
Any help is appreciated,
Thanks,
df