一、导入数据
数据是模拟生成的一份虚拟数据:
利用这份数据做一些分析
二、贡献分析
1. 按产品子类计算各子类的销售百分比贡献
代码:
select product_sub_category,sum(sales) as sales,ROUND(sum(sales)*100/(select sum(sales) from data1),2) as percent
from data1
group by product_sub_category;
结果:
2. 按产品子类和渠道统计销售百分比
代码:
select product_sub_category,channel,sum(sales) as sales,ROUND(sum(sales)*100/(select sum(sales) from data1),2) as percent
from data1
group by product_sub_category,channel;
结果:
3. 创建一个饼图代表产品子类的贡献百分比
将1得到的结果用来绘制饼图
4. 按年份来创建面积图显示不同产品子类销量
首先查询出目标数据:
select year,product_sub_category,sum(order_qty) as sales
from data1
group by year,product_sub_category
order by year;
将查询结果绘制面积图
三、排序分析
1. 销售最高的5个地区
select region, sum(sales) as region_sales
from data1
group by region
order by region_sales desc
limit 5;
2. 销售最高的5个品牌
select brand_name, sum(sales) as brand_sales
from data1
group by brand_name
order by brand_sales desc
limit 5;
3. 利润率前20%的产品
这个问题可以拆分成两步,先求出20%的产品是多少,比如若总共有100种产品,那20%的产品就是20种产品,所以先算出20%的产品是多少种产品
select count(*)*0.2 from (select * from data1 group by product_name) as tmp
得到产品20%是22种产品
查询利润率前22名的产品
select product_name, round(sum(profit)*100/sum(cost_of_sales),2) as rate_of_profit
from data1
group by product_name
order by rate_of_profit DESC
limit 22
4. 逐年逐月计算利润排行榜
set @curRank :=0;
select *,@curRank := @curRank+1 as rand
from (select year,Month(order_date) as month,sum(profit) as sum_profit
from data1
group by year, Month(order_date)
order by sum_profit desc) as tmp;
将查询得到得数据做时间序列得利润走势图,可以看到利润有很强得周期性
5. 按地区展示销售和利润排行榜
-- 按地区展示销售排行榜
set @curRank :=0;
select *,@curRank := @curRank+1 as rand
from(select region, sum(sales) as sum_sales
from data1
group by region
order by sum_sales desc) as tmp
同理可查得地区利润排行帮榜
试绘制地区利润和销售得柱状图