牛客网SQL大厂真题二刷小白白话总结(三)电商场景(某东商城)
三、电商场景(某东商城)
目录
SQL13 计算商城中2021年每月的GMV(简单)
SQL14 统计2021年10月每个退货率不大于0.5的商品各项指标(中等)
SQL15 某店铺的各商品毛利率及店铺整体毛利率(中等)
SQL16 零食类商品中复购率top3高的商品(中等)
SQL17 10月的新户客单价和获客成本(较难)
SQL13 计算商城中2021年每月的GMV(简单)
select date_format(event_time,"%Y-%m") as month,
sum(total_amount) as GMV
from
tb_order_overall
where status!=2 and year(event_time)=2021
group by month
having GMV>100000
order by GMV
很简单,就是限制条件不要遗漏就可以啦
SQL14 统计2021年10月每个退货率不大于0.5的商品各项指标(中等)
select product_id,round(ctr,3) as ctr,
round(cart_rate,3) as cart_rate,
round(payment_rate,3) as payment_rate,
round(refund_rate,3) as refund_rate
from
(select product_id,sum(if_click)/count(*) as ctr,if(sum(if_click)!=0,sum(if_cart)/sum(if_click),0) as cart_rate,if(sum(if_cart)!=0,sum(if_payment)/sum(if_cart),0) as payment_rate,if(sum(if_payment)!=0,sum(if_refund)/sum(if_payment),0) as refund_ratefrom tb_user_eventwhere date_format(event_time,"%Y-%m")='2021-10'group by product_idhaving refund_rate<=0.5 or sum(if_payment)=0) xorder by product_id
这一道题主要是对于分母为0的数据也要考虑到(但实际操作好像考不考虑答案都没区别)
日期=后面不要忘记加“”(这是我一开始报错的原因)
SQL15 某店铺的各商品毛利率及店铺整体毛利率(中等)
select product_id,concat(round(profit_rate,1),"%") as profit_rate
from
(select ifnull(product_id, '店铺汇总') as product_id,
100*(1-sum(in_price*cnt)/sum(price*cnt))as profit_rate
from (select product_id,in_price,cnt,price from tb_order_detailJOIN tb_product_info USING(product_id)JOIN tb_order_overall USING(order_id)
where shop_id=901 and date(event_time)>='2021-10-01')y
group by product_id
with rollup
having profit_rate > 24.9 OR product_id IS NULL
order by product_id) as x
这是第一种解法,采用了with rollup。
分析一下题目,比较难理解的是毛利率,原题目中给公式是:
商品毛利率=(1-进价/平均单件售价)*100%;
店铺毛利率=(1-总进价成本/总销售收入)*100%
进价很好理解in_price,但是分母的平均单件售价有点难处理,难道还要求每一件商品售出价格的平均值嘛?
考虑上下同时乘以“件数cnt”,也就是每件商品的总成本/每件商品的总售价,每件商品的总售价不就是实际售价*件数之和嘛?
因此:sum(in_price*cnt)/sum(price*cnt)
另外,这里还有求【店铺】的毛利率,这里直接在group by后面接with roll up即可,得到分组的全部数据的汇总信息,就为店铺毛利率。
SQL16 零食类商品中复购率top3高的商品(中等)
select x2.product_id,
round(count(distinct x1.uid)/count(distinct x2.uid),3) as repurchase_rate
from
(select product_id,uid,count(order_id) as times_buy from
tb_order_overall join tb_order_detail using(order_id)
where status=1 and timestampdiff(day,date(event_time),(select max(date(event_time)) from tb_order_overall))<90
group by product_id,uid
having times_buy>=2) x1
right join
(select product_id,uid
from tb_order_overall join tb_order_detail using(order_id)
where status=1 and timestampdiff(day,date(event_time),(select max(date(event_time)) from tb_order_overall))<90
group by product_id,uid) x2
on x1.product_id=x2.product_id
and x1.uid=x2.uid
group by x2.product_id
order by repurchase_rate desc, product_id
limit 3
感觉我写的特别复杂,主要就是两个表,一个是复购表,只保留了满足条件复购数量>=2的商品用户,还有一个表就是全部的用户,两个表各自uid的数目相除就是复购率。
参考一下评论区更简单的代码:
SELECT product_id,ROUND(SUM(repurchase) / COUNT(repurchase), 3) as repurchase_rate
FROM (SELECT uid, product_id, IF(COUNT(event_time)>1, 1, 0) as repurchaseFROM tb_order_detailJOIN tb_order_overall USING(order_id)JOIN tb_product_info USING(product_id)WHERE tag="零食" AND event_time >= (SELECT DATE_SUB(MAX(event_time), INTERVAL 89 DAY)FROM tb_order_overall)GROUP BY uid, product_id
) as t_uid_product_info
GROUP BY product_id
ORDER BY repurchase_rate DESC, product_id
LIMIT 3;
这是只有一个select子表,那就是每个用户,对应 每个产品是否复购(只用1和0来表示判断),核心思想在于uid【全都保留】,作为主键,这样直接加总就是总用户数量,对于分子使用if判断句的结果加总,简化了很多。
SQL17 10月的新户客单价和获客成本(较难)
select round(avg(total_amount),1) as avg_amount,
round(avg(cost),1) as avg_cost
from(select a.order_id,total_amount,(sum(price*cnt)-total_amount) as costfrom tb_order_detail a left join tb_order_overall bon a.order_id = b.order_idwhere date_format(event_time,'%Y-%m') = '2021-10'and (uid,event_time) in (select uid,min(event_time) from tb_order_overallgroup by 1)group by 1,2) x
这道题卡住我的在于如何处理“新客户首单”这个条件,因为是对查询订单的范围进行界定,这个event_time的又要根据不同用户最早下单时间而变化,因此这里where语句后面:
and (uid,event_time) in (select uid,min(event_time) from tb_order_overall
group by 1)
也就是uid和event_time同时满足()里面的条件。
另外这里还有一个坑,total_amount是非聚合函数,因此要在group by里面加上total_amount(也就是group by 1,2两个变量)。
牛客网SQL大厂真题二刷小白白话总结(三)电商场景(某东商城)相关推荐
- 牛客网SQL大厂真题二刷小白白话总结(二)用户增长场景(某度信息流)
二.用户增长场景(某度信息流) 目录 二.用户增长场景(某度信息流) SQL7 2021年11月每天的人均浏览文章时长(简单) SQL8 每篇文章同一时刻最大在看人数(中等) SQL9 2021年11 ...
- 牛客网SQL大厂真题二刷小白白话总结(五)某宝店铺分析(电商模式)
目录 SQL25 某宝店铺的SPU数量(简单) SQL26 某宝店铺的实际销售额与客单价(简单) SQL27 某宝店铺折扣率(中等) SQL28 某宝店铺动销率与售罄率(较难) SQL29 某宝店铺连 ...
- 牛客网SQL大厂真题—SQL158:每类视频近一个月的转发量/率
描述 用户-视频互动表tb_user_video_log id uid video_id start_time end_time if_follow if_like if_retweet commen ...
- 牛客网 2018校招真题 美团点评 K的倍数
Description 牛客网 2018校招真题 K的倍数 Solving Ideas sum[i + 1]: 表示序列p[0]...p[i]的和 从长度最大的子串开始判断,当剩余需要判断子串长度不可 ...
- 牛客网 2018校招真题 摩拜 排序次数
Description 牛客网 2018校招真题 排序次数 Solving Ideas 将数组a的元素拷贝到数组b 对数组b进行排序 对比数组b,统计数组a中已排序的元素个数 如:a = [2, 11 ...
- 牛客网 2018校招真题 滴滴出行 寻找丑数
Description 牛客网 2018校招真题 寻找丑数 Solving Ideas 参考<剑指offer>丑数 Time complexity : O(n)O(n)O(n) Space ...
- 牛客网 PTA乙级真题 1003 数素数
数素数 (20) 时间限制 1000 ms 内存限制 32768 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小) 题目描述 令Pi表示第i个素数.现任给两个正整数M &l ...
- 牛客网 2018校招真题 爱奇艺 最后一位
Description 牛客网 2018校招真题 最后一位 Solving Ideas 二分查找 Solution import java.io.BufferedReader; import java ...
- 牛客网 2018校招真题 京东 回文
Description 牛客网 2018校招真题 回文 Solving Ideas 计算以str[str.length() - 1]为结尾的最大的回文长度,从而判断最少需要追加多少个字母才能使整个串成 ...
最新文章
- 第9部分 备份与灾难恢复
- ASP .NET Core使用connection string连接MySQL/MariaDB,并设置UTF-8编码
- springboot2新版springcloud微服务,带你了解不一样的springboot2
- ML_Logistic_Regression
- [Vue.js]实战 -- 电商项目(八)
- 修改USB固件库的Customer_HID例程
- Codeforces Round #327 div2
- 54.购物流程(1)---simple product
- nexus下载地址分享
- 汽车can总线协议c语言,CAN总线自定义协议使用说明.pdf
- 《Machine Learning in Action》—— hao朋友,快来玩啊,决策树呦
- 服务器数据迁移:为知笔记私服数据迁移
- Theorem、Proposition、Lemma和Corollary等的解释与区别
- 重磅:2019 前端开发者进阶指南.pdf
- 服务器性能指标图英文翻译,技术性能指标,technic performance guildline,在线英语词典,英文翻译,专业英语...
- 云呐资产|令人满意的固定资产盘点系统方法
- Self-augmented Unpaired Image Dehazing via Density and Depth Decomposition程序运行记录
- php fpm failed,ubuntu环境下启动php-fpm失败Job for php-fpm.service failed...
- Java控制器controller_实现接口Controller定义控制器
- 内存的分配和回收实验(首次适配、下次适配、最佳适配、最坏适配)