SQL outlets sales trend analysis of actual combat

Part I: SQL combat I suppliers turnover analysis

Sentiment: Begin with the End hardened data is an essential quality of migrant workers, when a question raised by a business, data structure model brain had formed.

For any company to sell the main industry, the understanding of their company's sales trends is essential, based on knowledge of historical sales data, the company can clearly understand that the sales store and a reasonable goal of customized sales sales into the company's purpose.

Objective:

  • Learn sales shop
  • Develop a reasonable sales target

Analysis of indicators:

  • Total sales for each quarter of each supplier
  • Develop sales targets in accordance with the sales of the previous year and the history of the Year

Problems Drive: sales per annum for each vendor each quarter

Analysis goal refinement:

  • The final result is returned : supplier information and sales ( output results must include the four dimensions of information: vendor sales number + + + year quarter)
  • Polymerization categories: each quarter of the year

------------- HMB --------------

Case dismantling:

  • Select the final result (the SELECT ) - supplier information and sales
  • Suppliers annual sales each quarter is calculated:

1. Supplier quarter sales each year = vendor products total annual sales each quarter of the sum ( the GROUP BY / SUM )
2. annual total sales of individual products each quarter of each year quarter = product orders recorded consumption sum ( the GROUP BY / sUM )
3. Year-quarter orders recorded information = order date belongs year and quarter (** YEAR / CASE WHEN **)

Positioning data:

  • Provide details of each vendor vendor information table
  • Provide contact between the product and the supplier of supplier information table
  • Calculate the total sales for each product order information table
  • Providing detailed product information on the date of the order information table
    Here Insert Picture Descriptiondetails the steps and answer the relevant code:

# 1.计算每个订单的年份及季度信息

SELECT
	`订单号码`,
	`订单日期`,
# 新增辅助列
	YEAR(`订单日期`) AS '年份',
	CASE
		WHEN MONTH(`订单日期`) >= 10 THEN 4
		WHEN MONTH(`订单日期`) >= 7 THEN 3
		WHEN MONTH(`订单日期`) >= 4 THEN 2
		ELSE 1
	END AS '季度' # 新建的字段 `` or ''都没关系
FROM `订单信息`

OUTPUT:
Here Insert Picture Description


# 2.计算每样产品每年度每季度销售额
		SELECT
		b.`产品号码`,
		a.`年份`,
		a.`季度`,
		SUM(`产品单价`* `购买数量`) AS `产品销售额`
FROM `订单明细` AS b 
INNER JOIN
	(
# 计算每个订单的年份及季度信息
SELECT
	`订单号码`,
	`订单日期`,
# 新增辅助列
	YEAR(`订单日期`) AS '年份',
	CASE
		WHEN MONTH(`订单日期`) >= 10 THEN 4
		WHEN MONTH(`订单日期`) >= 7 THEN 3
		WHEN MONTH(`订单日期`) >= 4 THEN 2
		ELSE 1
	END AS '季度' # 新建的字段 `` or ''都没关系
FROM `订单信息`
	)	AS a
		ON a.`订单号码` = b.`订单号码`
		GROUP BY `产品号码`,`年份`,`季度`
		

OUTPUT:
Here Insert Picture Description


# 3.计算供应商每年度每季度的总销售额
	SELECT
	d.`供应商号码`,
	c.`年份`,
	c.`季度`,
	SUM(c.`产品销售额`) AS `供应商每年各季度总销售额`
	FROM `产品信息` AS d
INNER JOIN
	(
		# 计算每样产品每年度每季度销售额
		SELECT
		b.`产品号码`,
		a.`年份`,
		a.`季度`,
		SUM(`产品单价`* `购买数量`) AS `产品销售额`
FROM `订单明细` AS b 
INNER JOIN
	(
# 计算每个订单的年份及季度信息
SELECT
	`订单号码`,
	`订单日期`,
# 新增辅助列
	YEAR(`订单日期`) AS '年份',
	CASE
		WHEN MONTH(`订单日期`) >= 10 THEN 4
		WHEN MONTH(`订单日期`) >= 7 THEN 3
		WHEN MONTH(`订单日期`) >= 4 THEN 2
		ELSE 1
	END AS '季度' # 新建的字段 `` or ''都没关系
FROM `订单信息`
	)	AS a
		ON a.`订单号码` = b.`订单号码`
		GROUP BY `产品号码`,`年份`,`季度`
) AS c 
ON c.`产品号码` = d.`产品号码`
GROUP BY `供应商号码`,`年份`,`季度`

OUTPUT:
Here Insert Picture Description

# 4表拼接练习完成:大功告成

SELECT 
e.`供应商号码`,
f.`公司`,
e.`年份`,
e.`季度`,
e.`供应商每年各季度总销售额`
FROM `供应商信息` AS f 
INNER JOIN
(
# 计算供应商每年度每季度的总销售额
	SELECT
	d.`供应商号码`,
	c.`年份`,
	c.`季度`,
	SUM(c.`产品销售额`) AS `供应商每年各季度总销售额`
	FROM `产品信息` AS d
INNER JOIN
	(
		# 计算每样产品每年度每季度销售额
		SELECT
		b.`产品号码`,
		a.`年份`,
		a.`季度`,
		SUM(`产品单价`* `购买数量`) AS `产品销售额`
FROM `订单明细` AS b 
INNER JOIN
	(
# 计算每个订单的年份及季度信息
SELECT
	`订单号码`,
	`订单日期`,
# 新增辅助列
	YEAR(`订单日期`) AS '年份',
	CASE
		WHEN MONTH(`订单日期`) >= 10 THEN 4
		WHEN MONTH(`订单日期`) >= 7 THEN 3
		WHEN MONTH(`订单日期`) >= 4 THEN 2
		ELSE 1
	END AS '季度' # 新建的字段 `` or ''都没关系
FROM `订单信息`
	)	AS a
		ON a.`订单号码` = b.`订单号码`
		GROUP BY `产品号码`,`年份`,`季度`
) AS c 
ON c.`产品号码` = d.`产品号码`
GROUP BY `供应商号码`,`年份`,`季度`
) AS e 
ON e.`供应商号码` = f.`供应商号码`

OUTPUT:

Here Insert Picture Description

——— ———— ———— ———— —————— —————— —————— —————— —————— —————— —————

Into EXCEL or other visualization tools to visualize ......

Published 17 original articles · won praise 10 · views 1672

Guess you like

Origin blog.csdn.net/weixin_44976611/article/details/104858198