题解 | #店铺901国庆期间的7日动销率和滞销率#
店铺901国庆期间的7日动销率和滞销率
https://www.nowcoder.com/practice/e7837f66e8fb4b45b694d24ea61f0dc9
这个应该是更有逻辑的方法:
# 倒推法根据题目,构造新表,包含字段如下:
# 目标日期(十一3天),上线产品id,七天内销售产品id(无则null),七天内有销售的日期(辅助查看,不必要)
# 创建目标时间序列,利用recursive迭代函数、cast(exp as data_type)
with recursive t1(dt) as(
select cast('2021-10-01' as date)
union all
select dt + interval 1 day #根据需求定义日期增长公式
from t1
where dt<'2021-10-03') #设置条件,给函数定义出口,开区间
,t2 as ( #筛选出指定时间区间内各类产品的销售时间表
select product_id,date(event_time) event_time
from tb_product_info
join tb_order_detail using(product_id)
join tb_order_overall using(order_id)
where status=1
and date(event_time) between date_sub('2021-10-01',interval 6 day) and '2021-10-03')
,t3 as( #筛选出截至目标时间,上线的产品id
select dt,product_id as product_online
from t1
left join tb_product_info
on dt>=date(release_time))
select dt
,round(count(distinct product_id)/count(distinct product_online),3) as sale_rate
,round(1-count(distinct product_id)/count(distinct product_online),3) as unsale_rate
from t3
left join t2
on t3.product_online=t2.product_id #每日上线产品表与销量表左链接
and datediff(dt,event_time) between 0 and 6
group by dt

查看24道真题和解析