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

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

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

with inshop as(
    select product_id
    from tb_product_info 
    where shop_id = 901
)
, tb as(
    select order_id, product_id, date(event_time) dt
    from tb_order_overall too
    right join tb_order_detail tod 
    using(order_id)
    where product_id in (select product_id from inshop)
)
select * from (
    select '2021-10-01', round(count(distinct product_id)/(select count(*) from inshop), 3) as v, round(1 - count(distinct product_id)/(select count(*) from inshop), 3)from tb where dt between '2021-09-25' and '2021-10-01' having EXISTS (SELECT * FROM tb_order_overall WHERE DATE(event_time) = '2021-10-01')
    union
    select '2021-10-02', round(count(distinct product_id)/(select count(*) from inshop), 3) v, round(1 - count(distinct product_id)/(select count(*) from inshop), 3)from tb where dt between '2021-09-26' and '2021-10-02' having EXISTS (SELECT * FROM tb_order_overall WHERE DATE(event_time) = '2021-10-02')
    union
    select '2021-10-03', round(count(distinct product_id)/(select count(*) from inshop), 3) v, round(1 - count(distinct product_id)/(select count(*) from inshop), 3)from tb where dt between '2021-09-27' and '2021-10-03' having EXISTS (SELECT * FROM tb_order_overall WHERE DATE(event_time) = '2021-10-03')
) a 

不讲武德暴力破解!遇到这种讨厌的滑动窗口,天数少的话都可以这样干,这里是三天,写了一个的逻辑复制三个就行。

这道题我学到了having (select count(*) from tb_order_overall where date(event_time) = '2021-10-02') a > 0是不行的,不能直接跟子查询和判断。

having exists (select * from tb_order_overall WHERE DATE(event_time) = '2021-10-03')才可以

全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务