题解 | #店铺901国庆期间的7日动销率和滞销率#

店铺901国庆期间的7日动销率和滞销率

https://www.nowcoder.com/practice/e7837f66e8fb4b45b694d24ea61f0dc9

# 可以分类讨论,对于9-25--10-01
with t1 as (
    select count(product_id) as release_cnt
    from tb_product_info
    where shop_id = '901'
    and date_format(release_time, '%Y-%m-%d') <= '2021-10-01'
),t2 as (
    select count(DISTINCT product_id) as sale_cnt
    from (
        select a.product_id, a.order_id, b.shop_id
        from tb_order_detail as a
        left join tb_product_info as b
        on a.product_id = b.product_id
        where b.shop_id = '901'
    ) as c
    where order_id in (select order_id from tb_order_overall where date_format(event_time, '%Y-%m-%d') between '2021-09-25' and '2021-10-01')
),# 9-26--10-02
t3 as (
    select count(product_id) as release_cnt
    from tb_product_info
    where shop_id = '901'
    and date_format(release_time, '%Y-%m-%d') <= '2021-10-02'
),t4 as (
    select count(DISTINCT product_id) as sale_cnt
    from (
        select a.product_id, a.order_id, b.shop_id
        from tb_order_detail as a
        left join tb_product_info as b
        on a.product_id = b.product_id
        where b.shop_id = '901'
    ) as c
    where order_id in (select order_id from tb_order_overall where date_format(event_time, '%Y-%m-%d') between '2021-09-26' and '2021-10-02')
),# 9-27--10-03
t5 as (
    select count(product_id) as release_cnt
    from tb_product_info
    where shop_id = '901'
    and date_format(release_time, '%Y-%m-%d') <= '2021-10-03'
),t6 as (
    select count(DISTINCT product_id) as sale_cnt
    from (
        select a.product_id, a.order_id, b.shop_id
        from tb_order_detail as a
        left join tb_product_info as b
        on a.product_id = b.product_id
        where b.shop_id = '901'
    ) as c
    where order_id in (select order_id from tb_order_overall where date_format(event_time, '%Y-%m-%d') between '2021-09-27' and '2021-10-03')
),# 将三天的union起来
t7 as (
    select dt, round(sale_rate, 3) as sale_rate, round(1 - sale_rate, 3) as unsale_rate
    from (
        select '2021-10-01' as dt, sale_cnt / (select release_cnt from t1) as sale_rate
        from t2
        union all
        select '2021-10-02' as dt, sale_cnt / (select release_cnt from t3) as sale_rate
        from t4
        union all
        select '2021-10-03' as dt, sale_cnt / (select release_cnt from t5) as sale_rate
        from t6
    ) as g
)
select t7.*
from t7
right join (
    # 要判断那一天有没有店铺有销量,有销量的才输出
    select DISTINCT date_format(event_time, '%Y-%m-%d') as dt
    from tb_order_overall
    where date_format(event_time, '%Y-%m-%d') between '2021-10-01' and '2021-10-03'
) as e
on e.dt = t7.dt;




全部评论

相关推荐

小厂面经,也是我的处女面(30min)1.自我介绍2.spring&nbsp;boot的自动装配原理(好多类和接口的单词都忘了全称是啥了,就说了记得的单词,流程应该说对了吧)3.有用过redis吗?主要是用在实现什么功能(说了技术派用redis的zset来实现排行榜)5.有了解过Redisson吗?讲一下对于分布式锁的了解以及在什么场景下应用(说了秒杀场景)6.对mysql有了解吗?包括它的索引优化和创建(把想起来的全说了)7.了解设计模式吗?比如单例模式,为什么要使用单例模式,它的优点是什么(昨天刚看的设计模式)8.工厂模式有了解吗?主要的使用场景是?(也是昨天刚看的)9.场景题:有7个服务器,需要在早上十点定时的向数据库中的用户表中的用户发短信,如果做到发送的消息不重复,且如果发送失败了需要知道是到哪个用户失败了,这样下次就直接从这个用户开始(我答了用spring&nbsp;task来实现定时,用分布式锁来保证只有一份服务器可以发送消息,用消息队列来存储消息,然后用消息确认机制来保证错误信息的记录,以及在数据库或者业务层面完成消息消费的幂等性)10.场景题:如果在系统启动的时间就将数据库的所有用户相关的信息都读到一个hashmap中(这个没啥思路,没答好)27届的投了一个星期终于有一个面试了,大部分公司都只招26的
inari233:已oc,拒了
查看9道真题和解析
点赞 评论 收藏
分享
03-12 00:21
门头沟学院 Java
Yki_:以下条件优先录用: 喜欢去缅北当猪仔的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务