mysql练习题(20210819)

create table product (
zid varchar(20),
category_id int,
`operator` varchar(20),
sellerid int,
shop_name varchar(64)
);

insert into product(zid,category_id,`operator`,sellerid,shop_name) values('A1', 2, '张三', 4, '服装1');
insert into product(zid,category_id,`operator`,sellerid,shop_name) values('A2', 2, '张三', 4, '服装1');
insert into product(zid,category_id,`operator`,sellerid,shop_name) values('A3', 4, '李四', 2, '服装4');
insert into product(zid,category_id,`operator`,sellerid,shop_name) values('c1', 4, '赵二', 8, '服装3');
insert into product(zid,category_id,`operator`,sellerid,shop_name) values('c2', 6, '赵二', 6, '服装6');
insert into product(zid,category_id,`operator`,sellerid,shop_name) values('c3', 8, '李二', 10, '服装4');




create table category_info (
id int,
fist_name varchar(20),
second_name varchar(64)
);
insert into category_info(id,fist_name,second_name) values(2,'女装','女裙');
insert into category_info(id,fist_name,second_name) values(4,'女装','短袖');
insert into category_info(id,fist_name,second_name) values(6,'男装','上衣');
insert into category_info(id,fist_name,second_name) values(8,'男装','短裤');



create table `order` (
orderid varchar(20),
zid varchar(20),
quantity int,
`date` date,
gmv double,
buyerid varchar(20)
);


insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-001','A1',1,'2018-01-01', 156.7,'u_001');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-002','A2',1,'2018-06-01', 196.7,'u_002');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-003','A2',1,'2018-06-02', 176.7,'u_005');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-004','A3',1,'2018-06-17', 199.9,'u_006');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-005','A3',1,'2018-06-20', 1188.7,'u_007');

insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-006','c1',1,'2018-07-09', 166.8,'u_002');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-007','c2',1,'2018-07-10', 136.5,'u_005');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-008','A3',1,'2018-07-29', 123.8,'u_006');

insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-009','A2',1,'2018-08-10', 123.8,'u_007');


insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-010','A3',50,'2018-04-01', 2600.0,'u_003');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-011','A1',20,'2018-04-05', 2000.0,'u_004');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-020','A1',1,'2018-04-07', 20.0,'u_099');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-021','c1',1,'2018-04-07', 50.0,'u_098');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-021','c2',1,'2018-04-09', 30.0,'u_097');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-021','c3',20,'2018-04-09', 1999.0,'u_096');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-021','c3',2,'2018-04-09', 90.0,'u_095');


insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-012','c1',1,'2019-01-01', 19.5,'u_001');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-013','c2',1,'2019-02-01', 25.6,'u_011');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-014','c3',1,'2019-03-01', 126.7,'u_012');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-015','c1',1,'2019-04-01', 555.5,'u_013');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-033','A2',1,'2019-02-01', 10.9,'u_011');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-034','A3',1,'2019-03-01', 99.9,'u_012');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-035','A1',1,'2019-04-01', 299.9,'u_013');

insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-016','A1',8,'2020-01-01', 653.7,'u_001');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-017','A1',7,'2020-01-01', 328.7,'u_001');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-018','A3',6,'2020-02-01', 428.7,'u_008');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-019','A3',3,'2020-12-01', 25.7,'u_009');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-019','c2',2,'2020-12-01', 100.0,'u_021');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-019','c3',5,'2020-12-01', 100.0,'u_022');
insert into `order`(orderid,zid,quantity,`date`,gmv,buyerid) values('111-223-019','c3',5,'2020-12-01', 100.0,'u_022');

1.导出女装类目2020年整体销额,订单数,销量

-- 先求女装
select t2.zid
from category_info t1 left join product t2 on t1.id = t2.category_id
where t1.fist_name='女装'
;

-- 关联求聚合,注意:double会损失精度,替代使用decimal
select sum(gmv) as total_gmv,
       count(orderid) as order_cnt,
       sum(quantity) as total_quantity
from `order` t3
where t3.date >= '2020-01-01'
  and t3.date <= '2021-12-31'
  and t3.zid in (
    select t2.zid
    from category_info t1
             left join product t2 on t1.id = t2.category_id
    where t1.fist_name = '女装'
);

2.导出2018年4月总销额大于2000元的商品,整年的订单数,销量,购买用户数,总金额

-- 先求销售额大于2000的商品id(zid)
select zid,
       sum(gmv) as zid_total_gmv
from `order`
where year(date)='2018'
and month(date)='04'
group by zid
having zid_total_gmv > 2000;

-- 求 整年 的订单数,销量,购买用户数,总金额
select count(orderid)            as total_order,
       sum(quantity)             as total_quantity,
       count(distinct (buyerid)) as total_user,
       sum(gmv)                  as total_gmv
from `order`
where year(date) = '2018'
  and zid in (
    select zid,
           sum(gmv) as zid_total_gmv
    from `order`
    where year(date) = '2018'
      and month(date) = '04'
    group by zid
    having zid_total_gmv > 2000
)
;

3.导出2019年订单单价在如下价格阶段下商品的 总销量,总订单,总销额(0-20,20-50,50-100,100-200,200-500,500+)

-- 根据商品id分组聚合(这道题有点歧义,也就是是否要根据商品id分组)
select zid,
       sum(quantity) as total_quantity,
       count(orderid) as total_order,
       sum(gmv) as total_gmv,
       '0-20'
from `order`
where year(date)='2019'
  group by zid
having total_gmv < 20

union all
select zid,
       sum(quantity) as total_quantity,
       count(orderid) as total_order,
       sum(gmv) as total_gmv,
       '20-50'
from `order`
where year(date)='2019'
  group by zid
having total_gmv >= 20 and total_gmv < 50

union all
select zid,
       sum(quantity) as total_quantity,
       count(orderid) as total_order,
       sum(gmv) as total_gmv,
       '50-100'
from `order`
where year(date)='2019'
  group by zid
having total_gmv >= 50 and total_gmv < 100

union all
select zid,
       sum(quantity) as total_quantity,
       count(orderid) as total_order,
       sum(gmv) as total_gmv,
       '100-200'
from `order`
where year(date)='2019'
  group by zid
having total_gmv >= 100 and total_gmv < 200

union all
select zid,
       sum(quantity) as total_quantity,
       count(orderid) as total_order,
       sum(gmv) as total_gmv,
       '200-500'
from `order`
where year(date)='2019'
  group by zid
having total_gmv >= 200 and total_gmv < 500

union all
select zid,
       sum(quantity) as total_quantity,
       count(orderid) as total_order,
       sum(gmv) as total_gmv,
       '200-500'
from `order`
where year(date)='2019'
  group by zid
having total_gmv >= 500
;

4.导出2020年每个店铺销量前3名商品,并输出一级类目,运营,店铺等基本信息

-- 写法1
select t2.zid,
       t4.fist_name,
       t3.operator,
       t3.sellerid,
       t3.shop_name
from (
         select zid,
                sum(quantity) as zid_total_quantity
         from `order`
         where year(date) = '2020'
         group by zid
         order by zid_total_quantity desc
         limit 3
     ) t2
         left join product t3 on t2.zid = t3.zid
         left join category_info t4 on t3.category_id = t4.id
;


-- 写法2
-- 定义变量
set @crank = 0;
select t2.zid,
       t4.fist_name,
       t3.operator,
       t3.sellerid,
       t3.shop_name

from(
    select t1.zid,
       t1.zid_total_quantity,
       @crank := @crank + 1 as crank
from (
         select zid,
                sum(quantity) as zid_total_quantity
         from `order`
         where year(date) = '2020'
         group by zid
         order by zid_total_quantity desc
     )t1
        )t2 left join product t3 on t2.zid = t3.zid
left join category_info t4 on t3.category_id = t4.id
where t2.crank <= 3
;

5.导出在2018年6月下单的用户,分别在7月,8月,9月下单的购买的用户数

select buyerid,
       '07'
from `order` t2
where year (t2.date)='2018'
  and month (t2.date)='07'
  and buyerid in (
    select t1.buyerid
    from `order` t1
    where year (t1.date)='2018'
  and month (t1.date)='06'
    )

union all

select buyerid,
       '08'
from `order` t2
where year (t2.date)='2018'
  and month (t2.date)='08'
  and buyerid in (
    select t1.buyerid
    from `order` t1
    where year (t1.date)='2018'
  and month (t1.date)='06'
    )

union all
select buyerid,
       '09'
from `order` t2
where year (t2.date)='2018'
  and month (t2.date)='09'
  and buyerid in (
    select t1.buyerid
    from `order` t1
    where year (t1.date)='2018'
  and month (t1.date)='06'
    )

;
  1. 延伸,用户漏斗分析,也叫用户留存,如:6月下单的用户,7月还剩哪些,8月还剩哪些,9月还剩哪些
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,347评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,435评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,509评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,611评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,837评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,987评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,730评论 0 267
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,194评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,525评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,664评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,334评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,944评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,764评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,997评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,389评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,554评论 2 349

推荐阅读更多精彩内容

  • 121.银行账户概要 力扣银行 (LCB) 帮助程序员们完成虚拟支付。我们的银行在表 Transaction 中记...
    冰菓_阅读 561评论 0 0
  • 281.获取定长连续子序列 要求:从 savior 表中获取状态为 0 的 id,并且这些 id 能够组成长度为 ...
    冰菓_阅读 608评论 0 6
  • 课程内容 mysql分区 水平切分 垂直切分 1. mysql分区 1.1 什么是分区? mysql数据库中的数据...
    _不想翻身的咸鱼阅读 436评论 0 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,529评论 28 53
  • 人工智能是什么?什么是人工智能?人工智能是未来发展的必然趋势吗?以后人工智能技术真的能达到电影里机器人的智能水平吗...
    ZLLZ阅读 3,766评论 0 5