SQL window functions for real reports: LAG gaps, running totals and frames
Most reporting queries that end up as nested subqueries or app-side loops are one window function away from simple. A practical tour of LAG for gaps, running totals, moving averages and the frame clause defaults that quietly give wrong numbers.