题解 | #店铺901国庆期间的7日动销率和滞销率# 使用union all 很快就解决了

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

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

#没有复杂的语法开窗和case when,只需要其中一个部分就可以了 依次算出每天的动销率和滞销率 而且当天存在销量即可 不要复杂化问题!

select y1,y2,y3 from(
select '2021-10-01' y1,round(count(distinct(t1.product_id))/3,3) y2,round(1-count(distinct(t1.product_id))/3,3) y3,(select ifnull((select event_time from tb_order_overall where date_format(event_time,'%Y-%m-%d')='2021-10-01' limit 1),0)) pan 
from tb_product_info t1 left join tb_order_detail t2 on t1.product_id=t2.product_id
left join tb_order_overall t3 on t2.order_id=t3.order_id
where t1.shop_id=901 and t3.event_time between '2021-09-25 00:00:00'and'2021-10-01 23:59:59' 
union all
select '2021-10-02',round(count(distinct(t1.product_id))/3,3),round(1-count(distinct(t1.product_id))/3,3),(select ifnull((select event_time from tb_order_overall where date_format(event_time,'%Y-%m-%d')='2021-10-02' limit 1),0)) 
from tb_product_info t1 left join tb_order_detail t2 on t1.product_id=t2.product_id
left join tb_order_overall t3 on t2.order_id=t3.order_id
where t1.shop_id=901 and t3.event_time between '2021-09-26 00:00:00'and'2021-10-02 23:59:59'
union all

select '2021-10-03',round(count(distinct(t1.product_id))/3,3),round(1-count(distinct(t1.product_id))/3,3),(select ifnull((select event_time from tb_order_overall where date_format(event_time,'%Y-%m-%d')='2021-10-03' limit 1),0))     
from tb_product_info t1 left join tb_order_detail t2 on t1.product_id=t2.product_id
left join tb_order_overall t3 on t2.order_id=t3.order_id
where t1.shop_id=901 and t3.event_time between '2021-09-27 00:00:00'and'2021-10-03 23:59:59'
) as gg where pan<>0
全部评论
我也这样答的,但这样真的可以吗?有个疑问怎么判断的在售商品数一定为3,放在实际不需要考虑库存和销量的关系吗?
2 回复 分享
发布于 2023-07-18 11:25 四川
这个在售商品不一定是3的,要结合release_time来看吧?
1 回复 分享
发布于 02-29 15:09 辽宁
跟我的思路完全一致,我就是来题解找,看有没有跟我一样分开算再union的,哈哈,感觉这样思路比较清晰。不过缺点就是代码确实冗余比较多。不过,如果实际开发能写成函数,把所求日期当成参数的话,就很好用了
点赞 回复 分享
发布于 2022-06-11 17:18

相关推荐

HNU_fsq:建议直接出国,这简历太6了。自愧不如
点赞 评论 收藏
分享
7 1 评论
分享
牛客网
牛客企业服务