题解 | #SQL 14.统计2021年10月每个退货率不大于0.5的商品各项指标#

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

http://www.nowcoder.com/practice/cbf582d28b794722becfc680847327be

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

明确题意:

统计2021年10月每个有展示记录的退货率不大于0.5商品的各项指标:

商品点展比=点击数÷展示数;加购率=加购数÷点击数;成单率=付款数÷加购数;退货率=退款数÷付款数,当分母为0时整体结果记为0。

结果中各项指标保留3位小数,并按商品ID升序排序。


问题分解:

  • 计算各个维度的计数(生成子表t_product_index_cnt)
    • 筛选时间窗内的记录:where DATE_FORMAT(event_time, '%Y%m') = '202110'
    • 按商品ID分组:group by product_id
    • 统计各种计数:
      • 展示数(每条记录就是一次展示):COUNT(1) as show_cnt
      • 点击数:sum(if_click) as click_cnt
      • 加购数:sum(if_cart) as cart_cnt
      • 付款数:sum(if_payment) as payment_cnt
      • 退款数:sum(if_refund) as refund_cnt
  • 计算各种指标率(除了展示数其他均可能为0,要特殊处理!):
    • 点击率:click_cnt/show_cnt as ctr
    • 加购率:IF(click_cnt>0, cart_cnt/click_cnt, 0) as cart_rate
    • 付款率:IF(cart_cnt>0, payment_cnt/cart_cnt, 0) as payment_rate
    • 退款率:IF(payment_cnt>0, refund_cnt/payment_cnt, 0) as refund_rate
    • 都保留3位小数:ROUND(x, 3)
  • 筛选退款率不大于0.5的商品,需注意分母可能为0:where payment_cnt = 0 or refund_rate <= 0.5

细节问题:

  • 表头重命名:as
  • 按商品ID排序:order by product_id;

完整代码:

select product_id, round(click_cnt/show_cnt, 3) as ctr,
    round(IF(click_cnt>0, cart_cnt/click_cnt, 0), 3) as cart_rate,
    round(IF(cart_cnt>0, payment_cnt/cart_cnt, 0), 3) as payment_rate,
    round(IF(payment_cnt>0, refund_cnt/payment_cnt, 0), 3) as refund_rate
from (
    select product_id, COUNT(1) as show_cnt,
        sum(if_click) as click_cnt,
        sum(if_cart) as cart_cnt,
        sum(if_payment) as payment_cnt,
        sum(if_refund) as refund_cnt
    from tb_user_event
    where DATE_FORMAT(event_time, '%Y%m') = '202110'
    group by product_id
) as t_product_index_cnt
where payment_cnt = 0 or refund_cnt/payment_cnt <= 0.5
order by product_id;
SQL大厂真题 文章被收录于专栏

大厂真题手把手教你怎么解~

全部评论
你好,请问这里可以解释下吗? 筛选退款率不大于0.5的商品,需注意分母可能为0:where payment_cnt = 0 or refund_rate <= 0.5 为什么需要限制 where payment_cnt = 0, 然后这个和上面的代码 round(IF(payment_cnt>0, refund_cnt/payment_cnt, 0), 3) as refund_rate 不是冲突的吗?
4 回复 分享
发布于 2022-06-04 11:55
还是你做的比较难懂啊
1 回复 分享
发布于 2022-12-10 14:27 广东
您好!能请您看看我的代码嘛(//-//)我很疑惑,我倒数第二行的筛选条件“where refund_rate<=0.5”会把refund_rate=0.333变成了0.000;而且如果这个筛选条件变成“payment_rate>1”,它就会把payment_rate=0.750变成了1.000。请问您知道我是哪里出了错误嘛?非常感谢!! select * from (select product_id, round(click/shown,3) as ctr, round(if(click=0,0,cart/click),3) as cart_rate, round(if(cart=0,0,pay/cart),3) as payment_rate, round(if(pay=0,0,refund/pay),3) as refund_rate from (select product_id, count(1) as shown, sum(if_click) as click, sum(if_cart) as cart, sum(if_payment) as pay, sum(if_refund) as refund from tb_user_event where substring(event_time,1,7)='2021-10' group by product_id)a)b where refund_rate<=0.5 order by product_id
点赞 回复 分享
发布于 2022-06-10 09:46
为什么要生成子表呢?不懂,求大佬指教
点赞 回复 分享
发布于 2022-08-25 10:26 广东
确实忘记考虑payment = 0的情况
点赞 回复 分享
发布于 2023-05-21 16:00 浙江
为啥最后的where是这样的,限制refund rate<=0.5 的时候0不是小于0.5的吗
点赞 回复 分享
发布于 05-02 16:53 广东

相关推荐

已老实求offer😫:有点像徐坤(没有冒犯的意思哈)
点赞 评论 收藏
分享
把球:这个听过,你加了就会发现是字节的hr
点赞 评论 收藏
分享
30 6 评论
分享
牛客网
牛客企业服务