示例
示例
SELECT store_id, month, revenue,
LAG(revenue) OVER (PARTITION BY store_id ORDER BY month) AS prev_revenue,
ROUND(100.0 * (revenue - LAG(revenue) OVER (PARTITION BY store_id ORDER BY month))
/ NULLIF(LAG(revenue) OVER (PARTITION BY store_id ORDER BY month), 0), 2) AS mom_pct
FROM store_monthly;| 分区 | PARTITION BY 业务对象(门店/商品) |
|---|---|
| 排序 | ORDER BY 时间 |
| NULL | 首月无环比;分母为 0 用 NULLIF |