产品 销售表 t
xno(产品号) name (产品名) price(产品价格) 件数 hname(店名) pno (顾客名) sno(消费结算号)
x01001 女式外套 800 2 h001 p0001 s0001
x01003 女式短袖 400 1 h001 p0001 s0001
x01004 女式短裤 500 3 h002 p0001 s0002
x01005 女式毛衣 700 2 h003 p0002 s0003
x01006 女式长裤 600 4 h002 p0001 s0002
x01007 女式裙子 450 5 h003 p0002 s0003
x01008 男式外套 900 3 h004 p0004 s0005
x01009 男式短袖 350 2 h004 p0004 s0005
x010010 男式裤子 550 6 h004 p0005 s0006
x010011 男式毛衣 900 1 h004 p0005 s0006
x01001 女式外套 800 2 h001 p0006 s0007
x01003 女式短袖 400 6 h001 p0006 s0007
x01004 女式短裤 500 7 h002 p0001 s0008
x01005 女式毛衣 700 2 h003 p0007 s0009
x01006 女式长裤 600 3 h003 p0007 s0009
x01007 女式裙子 450 1 h003 p0002 s0010
门店信息表h
hno(店号) hname(店名) hlevel
h001 东街店 1
h002 南街店 2
h003 北街店 3
h004 西街店 1
顾客信息表p
pno pname plevel
p0001 张三 A
p0002 李四 B
p0003 王五 C
p0004 赵六 A
p0005 钱七 B
p0006 孙九 C
p0007 周十 A
要求统计其中四种产品在等级为1的门店的销售情况如下
产品编码 项目名称 A类顾客人次 B类顾客人次 A类顾客购买件数 B类顾客购买件数 A类顾客总收入 B类顾客总收入
x01003 女式短袖
x01004 女式短裤
x01005 女式毛衣
x01006 女式长裤
select t1.xno as 产品编码,
t1.name as 项目名称,
sum(Case when t2.plevel="A" then 1 else 0 end ) as A类顾客人次,
sum(Case when t2.plevel="B" then 1 else 0 end ) as B类顾客人次,
sum(Case when t2.plevel="A" then t1.件数 else 0 end ) as A类顾客购买件数,
sum(Case when t2.plevel="B" then t1.件数 else 0 end ) as B类顾客购买件数,
sum(Case when t2.plevel="A" then t1.件数*t1.price else 0 end ) as A类顾客总收入,
sum(Case when t2.plevel="B" then t1.件数*t1.price else 0 end ) as B类顾客总收入
from 产品销售表 t1
inner join 顾客信息表p t2 on t1.pno =t2.pno
inner join 门店信息表h t3 on t1.hname=t3.hno
where t3.hlevel="1" and t1.xno in ("x01003","x01004","x01005","x01006")
group by t1.xno, t1.name
t表中的件数字段暂定义为num
select t1.xno as 产品编码,
t1.name as 项目名称,
IFNULL(sum(Case when t2.plevel="A" then 1 else 0 end ),0) as A类顾客人次,
IFNULL(sum(Case when t2.plevel="B" then 1 else 0 end ),0) as B类顾客人次,
IFNULL(sum(Case when t2.plevel="A" then t1.num else 0 end ),0) as A类顾客购买件数,
IFNULL(sum(Case when t2.plevel="B" then t1.num else 0 end ),0) as B类顾客购买件数,
IFNULL(sum(Case when t2.plevel="A" then t1.num*t1.price else 0 end ),0) as A类顾客总收入,
IFNULL(sum(Case when t2.plevel="B" then t1.num*t1.price else 0 end ),0) as B类顾客总收入
from t as t1
inner join p as t2 on t1.pno =t2.pno
inner join h as t3 on t1.hname=t3.hno
where t3.hlevel="1" and t1.xno in ("x01003","x01004","x01005","x01006")
group by t1.xno, t1.name
select t1.xno as 产品编码,
t1.name as 项目名称,
sum(Case when t2.plevel="A" then 1 else 0 end ) as A类顾客人次,
sum(Case when t2.plevel="B" then 1 else 0 end ) as B类顾客人次,
sum(Case when t2.plevel="A" then t1.件数 else 0 end ) as A类顾客购买件数,
sum(Case when t2.plevel="B" then t1.件数 else 0 end ) as B类顾客购买件数,
sum(Case when t2.plevel="A" then t1.件数t1.price else 0 end ) as A类顾客总收入,
sum(Case when t2.plevel="B" then t1.件数t1.price else 0 end ) as B类顾客总收入
from 产品销售表 t1
inner join 顾客信息表p t2 on t1.pno =t2.pno
inner join 门店信息表h t3 on t1.hname=t3.hno
where t3.hlevel="1" and t1.xno in ("x01003","x01004","x01005","x01006")
group by t1.xno, t1.name