Deviopath › PostgreSQL

Day-over-Day Signups

Medium · Window Functions

LAG(x) OVER (ORDER BY d) returns x from the previous row (or NULL on the first row), which makes "change since last time" easy. Some days may be missing; compare with the previous recorded day.

Return day, signups, change (signups minus the previous day's signups) and pct_change, computed as ROUND((signups - previous) * 100.0 / previous, 1). Both are NULL on the first day, and pct_change is NULL when the previous day had 0 signups (use NULLIF(previous, 0)). Sort by day.

Next: Clean Up, Then Lock It Down