Hi there,
I've tried 2 different ways to do this, (below) but cant get to work.
I have one table [Activity-Opp-Data-Wenda] which is populated with OpportunityID - this is unique. I have another Table, (Activities) that contains Activity data: ActivityID (unique), OpportunityID, (not unique in this table) ActivityDate and Status - not unique.
Each Opportunity can have many Activities. I'd like to get the most recent, (ActivityDate) and the ActivityID from the Activities Table for each OpportunityID, WHERE the Status = 'Completed' - into [Activity-Opp-Data-Wenda].
Thank you for any time spent looking at this, you're a life saver
I've tried 2 different ways to do this, (below) but cant get to work.
I have one table [Activity-Opp-Data-Wenda] which is populated with OpportunityID - this is unique. I have another Table, (Activities) that contains Activity data: ActivityID (unique), OpportunityID, (not unique in this table) ActivityDate and Status - not unique.
Each Opportunity can have many Activities. I'd like to get the most recent, (ActivityDate) and the ActivityID from the Activities Table for each OpportunityID, WHERE the Status = 'Completed' - into [Activity-Opp-Data-Wenda].
Code:
UPDATE [Activity-Opp-Data-Wenda] INNER JOIN Activities ON [Activity-Opp-Data-Wenda].OpportunityID = Activities.OpportunityID SET [Activity-Opp-Data-Wenda].LastActivityID = Activities.ActivityID, [Activity-Opp-Data-Wenda].LastActivityDate = Activities.ActivityDate
WHERE Activities.OpportunityID & Activities.ActivityID & Activities.ActivityDate in (SELECT Activities.OpportunityID & Activities.ActivityID & MAX(Activities.ActivityDate) from Activities WHERE Activities.Status="Completed" GROUP BY Activities.OpportunityID,Activities.ActivityID);
Code:
UPDATE [Activity-Opp-Data-Wenda]
INNER JOIN (SELECT OpportunityID, ActivityID, MAX(ActivityDate) AS MaxDate FROM Activities WHERE Status="Completed" GROUP BY OpportunityID) AS [Max] ON ([Activity-Opp-Data-Wenda].OpportunityID = Max.OpportunityID) AND (Activities.ActivityDate = Max.MaxDate)
SET [Activity-Opp-Data-Wenda].LastActivityID = Activities.ActivityID, [Activity-Opp-Data-Wenda].LastActivityDate = Activities.ActivityDate
Thank you for any time spent looking at this, you're a life saver