如何计算销售额的同比和环比增长?

进阶SQL

答题要点

SELECT 
  month,
  sales,
  -- 环比增长(与上月比)
  LAG(sales, 1) OVER (ORDER BY month) AS last_month_sales,
  (sales - LAG(sales, 1) OVER (ORDER BY month)) / LAG(sales, 1) OVER (ORDER BY month) * 100 AS mom_growth,
  
  -- 同比增长(与去年同月比)
  LAG(sales, 12) OVER (ORDER BY month) AS last_year_sales,
  (sales - LAG(sales, 12) OVER (ORDER BY month)) / LAG(sales, 12) OVER (ORDER BY month) * 100 AS yoy_growth
FROM monthly_sales
ORDER BY month;

MoM (Month-over-Month):环比,mom_growth=(本月-上月)/上月×100% YoY (Year-over-Year):同比,yoy_growth=(本月-去年同月)/去年同月×100%

完整解析在题库里看

核心知识点、易错点和面试官想听到什么;练过的题会进入记录,答错的进错题本。

去练这道题

回知识库看这一块

同一能力的其他题