sql语句查询同比、环比

sql语句查询同比增量、同比

SELECT
	a.customer,
	a.month,
	( a.x - b.y ) 同比增量,
	( a.x - b.y ) / b.y * 100 同比 
FROM
	( SELECT customer, year, month, SUM( number ) x FROM syxy_sales WHERE year = 2020 GROUP BY month, customer ) a
	LEFT JOIN ( SELECT customer, year, month, SUM( number ) y FROM syxy_sales WHERE year = 2019 GROUP BY month, customer ) b ON a.month = b.month 
GROUP BY
	a.customer

在这里插入图片描述
sql语句查询同比增量、同比

SELECT
	a.customer,
	a.month,
	( a.x - b.y ) 环比增量 ,
	( a.x - b.y ) / b.y 环比 
FROM
	( SELECT customer, month, SUM( number ) x FROM syxy_sales WHERE year = 2019 GROUP BY month, customer ) a
	LEFT JOIN ( SELECT customer, month, SUM( number ) y FROM syxy_sales WHERE year = 2019 GROUP BY month, customer ) b ON a.month = b.month + 1 
GROUP BY
	a.customer,
	a.month

在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页