Use SQL suppliers turnover for analysis

Real case:

Learn how to focus and how to refine the analysis target dismantling Case

learning target:

Master complex issues and the gradual dismantling of thinking ability, so that problem-solving ideas traceable
to simulate the real work cases, make real ground data analysis, effective

Objective:

  • Sales understanding cooperation of suppliers of products
  • Improve procurement efficiency, enhance relationships with large suppliers
  • Increase the company's sales and profits

Analysis of indicators:

  • The total turnover of the supplier of goods
  • The higher the total sales, the larger suppliers

Problems Drive : find total sales of the top 10 suppliers of information

Analysis goal refinement:

  • The final result is returned: Vendor Information
  • Filtering data: total sales Suppliers
  • Filter: the highest sales 10

Case dismantling

  • Select the final result (the SELECT) - Supplier Information
  • Filtering data - the total amount calculated supplier
    • A single product = total sales of consumer goods orders recorded and (GROUP BY / SUM)
    • Suppliers Sales = total sales of suppliers and (GROUP BY / SUM)
  • Filtering conditions (ORDER BY / LIMIT) - 10 highest sales

Positioning data:

  • Provide details of each vendor vendor information table
  • Provide contact between the product and the supplier of the product information sheet
  • It calculates the total sales for each product order information table

Here Insert Picture Description

A deep understanding of the connotation of the business relationship between each table and the table and the table:

For example, Order Details characterization of every order, sales records details of all types of products, so the product can cluster number, used to calculate the total sales of each product.

A clear logic:

Each vendor can provide multiple products, or that different products may come from the same supplier; each record may include multiple orders Order Details record; so to get to filter data: supplier of total sales, respectively, come get the total sales for each product, and then get the total sales for each vendor. Then you do sorting filter, connected to the operating table, to obtain vendor information 10 before total sales.

Analysis goal : total sales of the top 10 listed suppliers.

Detailed answers steps:

  • Total sales calculated for each kind of product - `` `Order Details table: GROUP BY / SUM
SELECT 
	`产品号码`, 
	SUM( `产品单价` * `购买数量` ) AS `产品销售额` 
	FROM `订单明细` 
	GROUP BY `产品号码`
  • For each vendor computing history total sales, vendor information obtained before 10
SELECT
	a.`供应商号码`,
	SUM( `产品销售额` ) AS `供应商销售额` 
FROM `产品信息` AS a
	INNER JOIN (
	SELECT 
	`产品号码`, 
	SUM( `产品单价` * `购买数量` ) AS `产品销售额` 
	FROM `订单明细` 
	GROUP BY `产品号码` ) AS b 
ON a.`产品号码` = b.`产品号码` 
GROUP BY
	a.`供应商号码` 
ORDER BY
	`供应商销售额` DESC
  • Suppliers provide historical total sales number, company, and the total sales of the top 10 as the final results.
# V2 优化版本

SELECT
c.`供应商号码`,
d.`公司`,
c.`供应商销售额`
FROM `供应商信息` AS d
INNER JOIN
(
	SELECT
		a.`供应商号码`,
		SUM( `产品销售额` ) AS `供应商销售额` 
	FROM `产品信息` AS a
	INNER JOIN
	(
		SELECT 
			`产品号码`, 
			SUM( `产品单价` * `购买数量` ) AS `产品销售额` 
		FROM `订单明细` 
		GROUP BY `产品号码` ) 
AS b 
ON a.`产品号码` = b.`产品号码` 
GROUP BY a.`供应商号码` 
ORDER BY `供应商销售额` DESC
LIMIT 10
) AS c
ON c.`供应商号码` = d.`供应商号码`

# 子查询只返回需要的字段;改变连接的逻辑顺序,代码变短;
# JOIN语句改成 LEFT JOIN

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

SQL code to achieve:

# V1 版本
SELECT
	d.`供应商号码`,
	e.`公司`,
	d.`供应商销售额` 
FROM
	(
SELECT
	`供应商号码`,
	SUM( `产品销售额` ) AS `供应商销售额` 
FROM
	(
SELECT
	a.`产品号码`,
	b.`供应商号码`,
	a.`产品销售额` 
FROM
	( 
	SELECT `产品号码`, 
	SUM( `产品单价` * `购买数量` ) AS `产品销售额` 
	FROM `订单明细` 
	GROUP BY `产品号码` ) AS a
	LEFT JOIN `产品信息` AS b 
	ON a.`产品号码` = b.`产品号码` 
	) AS c 
GROUP BY `供应商号码` 
ORDER BY `供应商销售额` DESC 
	LIMIT 10 
	) AS d
	LEFT JOIN `供应商信息` AS e 
	ON d.`供应商号码` = e.`供应商号码`

Here Insert Picture Description

The results Visualization:
Here Insert Picture Description
Here Insert Picture Description

Published 17 original articles · won praise 10 · views 1673

Guess you like

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