data
import database
First import the database:
import separately:
as follows:
import several other data tables in the same way:
understand data
Category: cate Brand: brand Sales: qtty Sales: amount User ID: pin Product ID: SKUID
- Pin table : three characteristic relationships of dt, skuid, and pin. Namely: time, user ID and item ID.
- sku table : the relationship between skuid, cate, and brand features. That is, the relationship between the product number, category, and brand.
- Order table : The relationship between dt, skuid, sku_name, qtty, and amount features. Namely: time, commodity number, commodity name, sales, sales.
A category is a category under a brand. Such as Huawei laptops.
Require
1. All products with a unit price of 0 are not included in sales . 2. Notebooks with
a unit price of less than 500 are not included in sales
.
The number of de-duplicated users of the brand under the category (for example, a user id browses three products of Xiaomi in a single day, the number of visitors to the Xiaomi brand under this category is recorded 1)
5, 2019 and 2018 sales are the sales of No.
1-156 、 Year- on-year data can be presented in
decimal form, keep two digits
ideas
- The order table is associated with the sku table to take out the brand of the order
- Use if in sum to judge whether it is 19 years old
- The first two are where filtering, followed by case when renaming, first filtering in the subquery, this mainly examines the code style
my code
SELECT
result2.*,
ROUND( result2.19年销量 / result2.18年销量, 2 ) AS 19年销量同比,
ROUND( result2.19年销额 / result2.18年销额, 2 ) AS 19年销额同比,
ROUND( result2.19年访客量 / result2.18年访客量, 2 ) AS 19年访客量同比
FROM
(
SELECT
result.cate 品类,
result.brand 品牌,
SUM(
CASE
WHEN YEAR ( result.order_dt ) = 2019
AND result.unit_price > 0
AND result.sku_name NOT LIKE '%补差价%'
AND DAY ( result.order_dt ) BETWEEN 1
AND 15
AND ( CASE WHEN result.cate = '笔记本' AND result.unit_price < 500 THEN NULL ELSE result.qtty END ) THEN
result.qtty ELSE NULL
END
) AS 19年销量,
SUM( CASE WHEN YEAR ( result.order_dt ) = 2019 AND result.sku_name NOT LIKE '%补差价%' THEN result.amount ELSE NULL END ) 19年销额,
COUNT( DISTINCT CASE WHEN YEAR ( result.pin_dt ) = 2019 AND result.sku_name NOT LIKE '%补差价%' THEN result.pin ELSE NULL END ) AS 19年访客量,
SUM(
CASE
WHEN YEAR ( result.order_dt ) = 2018
AND result.unit_price > 0
AND result.sku_name NOT LIKE '%补差价%'
AND DAY ( result.order_dt ) BETWEEN 1
AND 15
AND ( CASE WHEN result.cate = '笔记本' AND result.unit_price < 500 THEN NULL ELSE result.qtty END ) THEN
result.qtty ELSE NULL
END
) AS 18年销量,
SUM( CASE WHEN YEAR ( result.order_dt ) = 2018 AND result.sku_name NOT LIKE '%补差价%' THEN result.amount ELSE NULL END ) 18年销额,
COUNT( DISTINCT CASE WHEN YEAR ( result.pin_dt ) = 2018 AND result.sku_name NOT LIKE '%补差价%' THEN result.pin ELSE NULL END ) AS 18年访客量
FROM
(
SELECT
sku.cate,
CASE
WHEN `order`.sku_name LIKE '%荣耀%' THEN
'华为' ELSE sku.brand
END brand,
`order`.skuid,
`order`.qtty,
`order`.amount,
`order`.sku_name,
`order`.amount / `order`.qtty AS unit_price,
`order`.dt AS order_dt,
pin.pin,
pin.dt AS pin_dt
FROM
sku
JOIN `order` ON sku.skuid = `order`.skuid
JOIN pin ON sku.skuid = pin.skuid
) AS result
GROUP BY
result.cate,
result.brand
) AS result2;
result: