About this question
I'm trying to understand how to filter my subquery in context to the main query. Ultimately I'm trying to get the MAX value from the latest record BEFORE the date of the current record. Here is where I'm at.
UPDATE Opphistory t
SET MaxStageSortOrder = sub.max_snapshotdate
FROM (
SELECT opportunityid, max(snapshotdate) AS max_snapshotdate
FROM Opp History
WHERE forecastcategory <> 'Omitted' and snapshotdate <= t.snapshotdate
GROUP BY 1
) sub
WHERE t.opportunityid = sub.opportunityid
It is the snapshotdate <= t.snapshotdate that appears to fail.