Module 07 Summary
The idea this module was built around
GROUP BY collapses rows; a window function computes across rows and keeps every one. That difference is what makes running totals, rankings, and per-row share columns possible at all.
What you can now do
- Add a total, rank, or running sum to a result without losing the underlying rows
- Control which rows a calculation sees, using
PARTITION BYand ordering - Compute a percentage-of-total whose denominator is found by the query, not typed in
The trap this module removed
Hardcoding a denominator produces a query that is right today and quietly wrong tomorrow. Percentages computed against a pasted-in total keep looking plausible long after they stop summing to 100 - there is no error, just drift. SUM(...) OVER () finds the denominator every time it runs.
Figures worth remembering
Daily accumulation opens at ₹99,801 on 1 June, reaching ₹1,96,323 by the 2nd and ₹2,94,066 by the 3rd, and must close at exactly ₹26,85,905 on 30 June. That closing check is what proves your window covered every row once and only once.
Country shares: IN 67.8% - GB 19.1% - SG 7.0% - not recorded 6.1% - summing to 100.0%.
Before you move on
Whenever you write a window function, name the check that would prove it right. For a running total it is "does the last row equal the grand total?". If you cannot name such a check, you do not yet know what the window is doing.
