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

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

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

# 3.4 9:05-    826
# 字段:dt,sale_rate,unsale_rate
# 时间:2021.10.01-2021.10.03 7日的动销率/滞销率(9.25-10.3)

# 动销率 = 有销量商品 / 上架总商品数 
# 滞销率 = 没有销量商品 / 上架总商品数

# tb1:链接 
with tb1 as(
select date(event_time) as dt,product_id
from tb_order_detail left join tb_order_overall using(order_id)
left join tb_product_info using(product_id)
where shop_id = 901 AND status =1 
order by dt,product_id
),

tb2 as(
select '2021-10-01' as dt,
# 动销率
round(count(distinct product_id)/(
select count(distinct product_id)
from tb_product_info 
where date(release_time)<=date_sub(date_format('2021-10-01','%Y-%m-%d'),interval 6 day) AND shop_id = 901
) ,3) as sale_rate,
# 滞销率
1-round(count(distinct product_id)/(
select count(distinct product_id)
from tb_product_info 
where date(release_time)<=date_sub(date_format('2021-10-01','%Y-%m-%d'),interval 6 day) AND shop_id = 901
) ,3) as unsale_rate
from tb1
where dt>=date_sub(date_format('2021-10-01','%Y-%m-%d'),interval 6 day) 
AND dt<= date_format('2021-10-01','%Y-%m-%d')
union all 
select '2021-10-02' as dt,
# 动销率
round(count(distinct product_id)/(
select count(distinct product_id)
from tb_product_info 
where date(release_time)<=date_sub(date_format('2021-10-02','%Y-%m-%d'),interval 6 day) AND shop_id = 901
) ,3) as sale_rate,
# 滞销率
1-round(count(distinct product_id)/(
select count(product_id)
from tb_product_info 
where date(release_time)<=date_sub(date_format('2021-10-02','%Y-%m-%d'),interval 6 day) AND shop_id = 901
) ,3) as unsale_rate
from tb1
where dt>=date_sub(date_format('2021-10-02','%Y-%m-%d'),interval 6 day) 
AND dt<= date_format('2021-10-02','%Y-%m-%d')

union all 
# 分日统计:10.03
select '2021-10-03' as dt,
# 动销率
round(count(distinct product_id)/(
select count(product_id)
from tb_product_info 
where date(release_time)<=date_sub(date_format('2021-10-03','%Y-%m-%d'),interval 6 day) AND shop_id = 901
) ,3) as sale_rate,
# 滞销率
1-round(count(distinct product_id)/(
select count(product_id)
from tb_product_info 
where date(release_time)<=date_sub(date_format('2021-10-03','%Y-%m-%d'),interval 6 day)  AND shop_id = 901
) ,3) as unsale_rate
from tb1
where dt>=date_sub(date_format('2021-10-03','%Y-%m-%d'),interval 6 day) 
AND dt<= date_format('2021-10-03','%Y-%m-%d')
)
select * 
from tb2 
# 筛选任意一间店铺有出单记录
where dt in (select date(event_time) from tb_order_overall)
# 动销率为0 如果当天有销售记录则输出,没有记录则不输出



全部评论

相关推荐

伟大的烤冷面被普调:暨大✌🏻就是强
点赞 评论 收藏
分享
和蔼:在竞争中脱颖而出,厉害! 但是有一个小问题:谁问你了?😡我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务