Day05 早自习

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Day05 早⾃习

SQL169 统计2021年10⽉每个退货率不⼤于
0.5的商品各项指标

https://www.nowcoder.com/practice/cbf582d28b794
722becfc680847327be?tpId=268&tqId=2285516&ru=
/exam/oj&qru=/ta/sql-factory-interview/question-ran
king&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26t
ab%3DSQL%25E7%25AF%2587%26topicId%3D268

知识点:
1. date_format(⽇期,”格式“) ⽇期格式化

格式: %Y 代表4位年, %y 两位年, %m ⽉, %d⽇


2. round(数值,⼩数位数) 四舍五⼊保留指定的⼩数位

3. having 条件 对分组后的数据进⾏过滤
#代码
select product_id,
round(sum(if_click)/count(id),3)
ctr,
round(sum(if_cart)/sum(if_click),3)
cart_rate,

round(sum(if_payment)/sum(if_cart),3)
payment_rate,

round(sum(if_refund)/sum(if_payment),3)
refund_rate
from tb_user_event
where DATE_FORMAT(event_time,'%Y-%m') =
'2021-10'
group by product_id
having refund_rate <= 0.5
order by product_id

#数据准备
DROP TABLE IF EXISTS tb_user_event;
CREATE TABLE tb_user_event (
id INT PRIMARY KEY AUTO_INCREMENT
COMMENT '⾃增ID',
uid INT NOT NULL COMMENT '⽤户ID',
product_id INT NOT NULL COMMENT '商品
ID',
event_time datetime COMMENT '⾏为时间',
if_click TINYINT COMMENT '是否点击',
if_cart TINYINT COMMENT '是否加购物⻋',
if_payment TINYINT COMMENT '是否付款',
if_refund TINYINT COMMENT '是否退货退款'
) CHARACTER SET utf8 COLLATE utf8_bin;

INSERT INTO tb_user_event(uid, product_id,


event_time, if_click, if_cart, if_payment,
if_refund) VALUES
(101, 8001, '2021-10-01 10:00:00', 0, 0,
0, 0),
(102, 8001, '2021-10-01 10:00:00', 1, 0,
0, 0),
(103, 8001, '2021-10-01 10:00:00', 1, 1,
0, 0),
(104, 8001, '2021-10-02 10:00:00', 1, 1,
1, 0),
(105, 8001, '2021-10-02 10:00:00', 1, 1,
1, 0),
(101, 8002, '2021-10-03 10:00:00', 1, 1,
1, 0),
(109, 8001, '2021-10-04 10:00:00', 1, 1,
1, 1);
SQL170 某店铺的各商品⽑利率及店铺整体
⽑利率

https://www.nowcoder.com/practice/65de67f666414
c0e8f9a34c08d4a8ba6?tpId=268&tqId=2285517&ru=
/exam/oj&qru=/ta/sql-factory-interview/question-ran
king&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26t
ab%3DSQL%25E7%25AF%2587%26topicId%3D268
知识点:

1. having 条件 对分组后的数据进⾏过滤
2. union 数据集合并
3. 值 as 字段名 常数列创建
4. concat (str1,str2,...) 字符串连接

select '店铺汇总' as product_id,


concat(round((1-
sum(in_price*cnt)/sum(price*cnt))*100,1),'%
') as profit_rate
from tb_product_info
inner join tb_order_detail on
tb_product_info.product_id =
tb_order_detail.product_id
and shop_id=901 # 产品表和订单总详情表连接,
过滤901店铺的订单数据
inner join tb_order_overall on
tb_order_detail.order_id =
tb_order_overall.order_id
and event_time>='2021-10-01' #和订单表进⾏连
接,过滤21年10⽉以来的数据
union
(select tb_product_info.product_id as
product_id,
concat(round((1-
sum(in_price*cnt)/sum(price*cnt))*100,1),'%
') as profit_rate
from tb_product_info
inner join tb_order_detail on
tb_product_info.product_id =
tb_order_detail.product_id
and shop_id=901 # 产品表和订单总详情表连接,
过滤901店铺的订单数据
inner join tb_order_overall on
tb_order_detail.order_id =
tb_order_overall.order_id
and event_time>='2021-10-01' #和订单表进⾏连
接,过滤21年10⽉以来的数据
group by tb_product_info.product_id
having 1-
sum(in_price*cnt)/sum(price*cnt)>0.249
order by product_id
)

代码优化

知识点:

1. with 临时表名 as (select 语句) 把select语句结果作


为⼀个临时表,供下半部分语句查询使⽤

with temp as
(select tb_product_info.product_id,
in_price,price, cnt
from tb_product_info
inner join tb_order_detail on
tb_product_info.product_id =
tb_order_detail.product_id
and shop_id=901 # 产品表和订单总详情表连接,
过滤901店铺的订单数据
inner join tb_order_overall on
tb_order_detail.order_id =
tb_order_overall.order_id
and event_time>='2021-10-01' )
select '店铺汇总' as pproduct_id,
concat(round((1-
sum(in_price*cnt)/sum(price*cnt))*100,1),'%
') as profit_rate
from temp
union
(select product_id as pproduct_id,
concat(round((1-
sum(in_price*cnt)/sum(price*cnt))*100,1),'%
') as profit_rate
from temp
group by product_id
having 1-
sum(in_price*cnt)/sum(price*cnt)>0.249
order by pproduct_id
)

You might also like