Problems / Peak and Drawdown / Editorial
MAX(value) OVER (PARTITION BY portfolio_id ORDER BY value_day ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
(peak - value) / peak * 100
0.00
Drawdown is only meaningful causally — each day compared to the peak *as of that day* — and the expanding window frame is SQL's encoding of causality. The tempting GROUP BY maximum leaks the future: early days get judged against highs that hadn't occurred yet, understating historical risk. Running MIN/MAX extremes generalize the running-SUM intuition (record-to-date, worst-case-to-date, high-water-mark billing), and pairing the windowed extreme with plain row arithmetic afterward — CTE, then percentage — keeps the query legible instead of nesting windows inside expressions.
GROUP BY
Solve Peak and Drawdown yourself →