Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

Explain postgres update select with subquery referencing the main table?

Asked by Ella Clarkson Mar 7, 2023 1.1K views 1 answer
Share

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.

Your answer

1 Answer

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.