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

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

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

select dt, coalesce(sum(sale_rate),0.000) as sale_rate, coalesce(sum(unsale_rate),1.000) as unsale_rate
from (
select  dt_group as dt,
    shop_id,
    round(count(distinct if(shop_id='901', t1.product_id, null)) / if(shop_id='901',t1.ccnntt,null),3) as sale_rate,
    round(1-count(distinct if(shop_id='901', t1.product_id, null)) / if(shop_id='901',t1.ccnntt,null),3) as unsale_rate
from
(
select a.product_id,c.cnt,a.shop_id
    ,date_format(b.event_time,'%Y-%m-%d') as dt_0
    ,ccnntt
from tb_order_overall b 
 join tb_order_detail c 
on b.order_id = c.order_id
and date_format(b.event_time,'%Y-%m-%d') between '2021-09-25' and '2021-10-03'
 join (select product_id,shop_id,count(1) over(partition by shop_id) as ccnntt from tb_product_info
) a
on a.product_id = c.product_id
) t1
left join (
    select distinct date_format(event_time,'%Y-%m-%d') as dt_group
    from tb_order_overall
    where date_format(event_time,'%Y-%m-%d') between '2021-10-01' and '2021-10-03'
) t2
on t1.dt_0 >= date_sub(t2.dt_group,Interval 6 day) and t1.dt_0 <= t2.dt_group
group by dt_group,t1.ccnntt,shop_id
)d  group by dt
order by dt

题目本身没啥,关键就是各种小需求给人使绊子,做的让人窝火;

值得注意的是,当你想用count(distinct) 开窗无果的时候,应该考虑用笛卡尔放大,多出来的一列作为group

全部评论

相关推荐

10-11 17:45
门头沟学院 Java
走吗:别怕 我以前也是这么认为 虽然一面就挂 但是颇有收获!
点赞 评论 收藏
分享
头像
11-21 11:39
四川大学 Java
是红鸢啊:忘了还没结束,还有字节的5k 违约金
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务