目录
- 1、要完成的任务
- 2、认识数据
- 3、SQL数据加工
- 4、excel形成分析仪
1、要完成的任务
目标:结合SQL和excel实现餐饮业日销售情况分析仪,如下表:
认识分析仪:
切片器:店面
分为四部分:KPI 、组合图、饼图、数据透视表
KPI指标:
- 当天销售情况
- 当天桌子使用情况
- 当天每把椅子的使用情况
组合图:每小时的销售额与销量变化趋势
饼图:不同菜品下的销售额占比情况,销量占比情况
数据透视表:不同菜品下的销售额情况,销量情况
2、认识数据
表结构信息:
bill表中缺失金额,order表缺失店名信息 —bill表与order表连接一下可以解决
反映不同店的桌子信息:
shop表要补充总座位表
3、SQL数据加工
-
用orderdetail表创建单汇总金额表(OrderGroup)–计算每单号的总金额
以orderdetail表的billnumber单号字段为汇总依据,求出每条billnumber下pay的加总值。
新表字段:billnumber(单号)、pay(金额) -
用Bill表与OrderGroup表创建新单号详细表(NewBill)–newbill = bill+每单总金额+每单折扣后金额
以billnumber为关键字段关联两表,将OrderGroup表中的pay字段合并到Bill表中,并使用pay与billdiscount字段计算出折扣金额。
新表字段:所有Bill表中的字段、pay(金额)、rebate(折扣金额)
计算逻辑:Rebate = pay * billdiscount -
用Shopdetail表创建新店面情况表(NewShopDetail)–添加每家店的总座位数
在原有shopdetail表字段基础上计算并添加allseats字段
新表字段:所有ShopDetail表中的字段、allseats(总座位数)
计算逻辑:allseats = twotable * 2 + three * 3 + fourtable * 6 -
用OrderDetail表与Bill表创建新点菜明细表(NewOrderDetail)–给order表增加店名信息
以billnumber为关键字段关联两表,并用Bill表中的shopname与OrderDetail表中的所有字段组成新表
新表字段:shopname(店名)、OrderDetail表中的所有字段 -
用NewBill表与NewShopDetail表创建店汇总信息表(ShopTotal)
以shopname字段为关键字段关联两表,并以shopname字段为汇总条件,创建以下字段
新表字段:
店名: b.shopname
单数: b.billnumber的计数
人数: b.peoplecount的加总
折扣总金额: b.rebate的加总
店汇总金额: b.pay的加总
单均消费: b.pay的合计值/b.billnumber的计数值
人均消费: b.pay的合计值/b.peoplecount的合计值
总台数: s.alltable
总座位数: s.allseats
翻台率: b.billnumber的计数值/s.alltable (总单数/总桌数)
上座率: b.peoplecount的合计值/s.allseats
折扣率: b.rebate的合计值/b.pay的合计值
导入bill表:
create database CateringCase;
use CateringCase;
-- Bill table
create table Bill(
billdate date not null,
billnumber varchar(20) not null default '-',
shopname varchar(20) not null default '-',
billdiscount float not null default 0,
paytime time not null,
tablenumber int not null default 0,
peoplecount int not null default 0
);
#导入数据
load data local infile 'D:/liwork/data/-bill.csv'
into table Bill
fields terminated by ',';
select * from Bill;
导入OrderDetail表:
-- OrderDetail table
create table OrderDetail(
billnumber varchar(20) not null default '-',
detail varchar(20) not null default '-',
pay int not null default 0
);
#导入数据
load data local infile 'D:/liwork/data/-order.csv'
into table OrderDetail
fields terminated by ',';
select * from OrderDetail;
导入ShopDetail表:
-- ShopDetail table
create table ShopDetail(
ShopName varchar(20) not null default '-',
twotable int not null default 0,
threetable int not null default 0,
fourtable int not null default 0,
alltable int not null default 0
);
#导入数据
load data local infile 'D:/liwork/data/-shop.csv'
into table ShopDetail
fields terminated by ',';
select * from ShopDetail;
数据加工:
-- 数据加工
-- 创建单汇总金额表
Create table OrderGroup(
select billnumber, sum(pay) as pay from OrderDetail
group by billnumber
);
select * from OrderGroup;
-- 创建新单号详细表
Create table NewBill(
select b.*,o.pay,b.billdiscount * o.pay as rebate from bill as b left join ordergroup as o
on b.billnumber = o.billnumber);
select * from NewBill;
NewBill表:
-- 创建新店面情况表
create table NewShopDetail(
select *, (twotable * 2 + threetable * 3 + fourtable * 6) as allseats
from shopdetail as s);
select * from NewShopDetail;
NewShopDetail表:
-- 创建新点菜明细表
create table neworderdetail(
select b.shopname,o.* from orderdetail as o left join bill as b
on o.billnumber = b.billnumber
);
select * from neworderdetail;
neworderdetail表:
-- 创建店汇总信息表
create table ShopTotal(
select b.shopname as 店名, count(b.billnumber) as 单数,
sum(b.peoplecount) as 人数,sum(b.rebate) as 折扣总金额,sum(b.pay) as 店汇总金额,
sum(b.pay)/count(b.billnumber) as 单均消费,
sum(b.pay)/sum(b.peoplecount) as 人均消费,
s.alltable as 总台数,
s.allseats as 总座位数,
count(b.billnumber)/s.alltable as 翻台率,
sum(b.peoplecount)/s.allseats as 上座率,
sum(b.rebate)/sum(b.pay) as 折扣率
from newbill as b left join newshopdetail as s
on b.shopname = s.shopname
group by b.shopname);
select * from shoptotal;
shoptotal表:
4、excel形成分析仪
从excel中连接MySQL,导入数据后做数据透视图,不断调整