The best way I can explain this is in an example. Imagine I have a table of sales associates, clients, and third table that shows to which Sales Associate a Client has been assigned, and when. I have three tables:;
TABLE: SALESASSOCIATE
SalespersonID: int, primary key
SalesPersonName: varchar
TABLE: CLIENT
ClientID: int, primary key
ClientName: varchar
TABLE: ASSIGNEDASSOCIATES
AssignmentID: int, primary key
AssignmentEffectiveDate: datetime
ClientID: int (join to Client.ClientID)
SalesmanID: int (join to SalesAssociate.Salesperson
ID)
The "AssignedAssociates" table keeps a history of all Client-Associate assignments; when a change in an assignment is made there is no "update" to a table; there is simply a new row added to the AssignedAssociates table. The effective date may have been in the past, so I can't assume the highest Assignment ID is the most recent assignment.
So, each day, I'd like to run a query or stored procedure that will return every client and the associate to whom they have been most recently assigned. If no associate is assigned, it should return null (thus an outer join somewhere). The only way I can think of doing this is with subqueries, e.g., start with a MAX(AssignmentEffectiveDat
e) somehow, but I can't see how to get around grouping by clientID and SalesmanID, which will return multiple records.
By the way, I don't have the option to change the structure of the tables.
Start Free Trial