Advertisement

05.22.2008 at 05:44PM PDT, ID: 23426329
[x]
Attachment Details

Getting Most Recent Changes and related values from a set of tables

Asked by kmoloney in MS SQL Server, SQL Query Syntax, SQL Server 2005

Tags: SQL (T-SQL / Microsoft SQL Server (2005)), Mozilla Firefox 2.x

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.SalespersonID)

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(AssignmentEffectiveDate) 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
[+][-]05.22.2008 at 05:53PM PDT, ID: 21628727

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.22.2008 at 07:29PM PDT, ID: 21629038

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Query Syntax, SQL Server 2005
Tags: SQL (T-SQL / Microsoft SQL Server (2005)), Mozilla Firefox 2.x
Sign Up Now!
Solution Provided By: Thomasian
Participating Experts: 2
Solution Grade: A
 
 
[+][-]05.22.2008 at 11:36PM PDT, ID: 21629809

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628