SQL notes to self
I knew these things in the sense that I'd seen them before, but I had to re-learn them today.
How do you get a value from the previous row?
With "lag"
Given...
...you can do this:
There's also "lead" which works in the opposite direction.
How do you update a table with the result of a query?
With "merge"
There are other ways to do this, but this one's in the standard.
Pleasantly, the query chooses which rows to update, and what values to use.
Also works with "when not matched" and "delete" or "insert" at the end.