19.7 Running Totals and Moving Averages with Frame Clauses
Alright, let’s get our hands dirty with the real sorcery of window functions: frame clauses. You’ve seen PARTITION BY and ORDER BY; they’re the opening act. The frame clause is the headliner. It’s how you define a sliding window within your partition, and it’s the secret sauce behind running totals and moving averages. Think of it this way: PARTITION BY carves up your data into islands. ORDER BY lines up the houses on each island in a specific order. The frame clause then lets you walk down the street on that island and say, “For this house, I want to look at the house itself and the two houses before it.” Or, “For this house, I want to look at every house from the start of the island up to this one.” That’s the magic.