题解 | #店铺901国庆期间的7日动销率和滞销率#(感觉是自己做得最窝囊的一次)

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

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

这题反复折腾了挺长时间的,看了好多个题解,发现都没有和自己的想法一致的,但是在看的过程中,自己又有了思路,最后照着这种崎岖的思路写了下来,没想到还真的给过了,但是觉得这种解法实在是难登大雅之堂。或者说和我之前写过的SQL相比,这有点像是投机取巧。

#  动效率  = 有销量的商品/已上架总商品数
#  滞销率  = 没有销量的商品/已上架总商品数      
#  两者恰好互补 

#  统计出9.25--10.03 这九天的数据 近七天901店铺的 所有商品,有销售的就打个记号
with t1 as (
select tpi.product_id,
       tpi.shop_id,
       date_format(too.event_time,'%Y-%m-%d') as dt
from tb_order_overall too
join tb_order_detail tod using(order_id)
join tb_product_info tpi on tod.product_id = tpi.product_id
having dt >= '2021-09-25' and dt <='2021-10-03'
order by dt
),

t2 as( 
select distinct dt as dtt,
      (select count(distinct product_id) as dx
       from t1
       where timestampdiff(day,dt,'2021-10-01') <= 6 and timestampdiff(day,dt,'2021-10-01') >= 0 and shop_id = 901) as maichu
from t1
where dt = '2021-10-01' 

union all

select distinct dt as dtt,
      (select count(distinct product_id) as dx
       from t1
       where timestampdiff(day,dt,'2021-10-02') <= 6 and timestampdiff(day,dt,'2021-10-02') >= 0 and shop_id = 901) as maichu
from t1
where dt = '2021-10-02'

union all 

select distinct dt as dtt,
      (select count(distinct product_id) as dx
       from t1
       where timestampdiff(day,dt,'2021-10-03') <= 6 and timestampdiff(day,dt,'2021-10-03') >= 0 and shop_id = 901) as maichu
from t1
where dt = '2021-10-03')




select t2.dtt as dt,
       round(maichu/(select count(product_id) from tb_product_info tpi where shop_id = 901), 3) as sale_rate,
       (1 - round(maichu/(select count(product_id) from tb_product_info tpi where shop_id = 901), 3)) as unsale_rate
from t2
order by dt


# select dt,
#        max(dt) over (partition by dt order by dt rows 6 preceding) as maichu
# from t1










我先照着我的这个通过的代码的思路讲一下,然后再讲自己之前没能通过的思路。(代码块里面有一些#是我写给自己看的注释)

第一个t1表是:找出有售卖记录的商品的id,以及时间,shop_id是为了后续筛选店铺用的。(即考虑只要当天任一店铺有任何商品的销量就输出该天的结果,即使店铺901当天的动销率为0这一情况。)
t2就是直接找10月1号,2号,3号对应店铺累计七天销量,dtt是时间,maichu是销量。我觉得这样做很傻,有点像小学生拿笔去一天一天的对应表格去数数据,不像之前做窗口函数,可以直接做一个滑动窗口一串代码,就可以把七天的数据都统计出来了。(但是我也暂时只会这种的办法了。。。。还是看了题解之后的。。。。。。)因为只需要统计三天的数据,所以连续复制粘贴两次就可以搞定了。
有了这两个表之后,后续就比较简单了,只需要简单的找出数据再运算一下就可以了。还有就是如何判断商品是否下架题目没有给出明确的定义,所以我再统计901店铺有多少在售商品的时候,没有写得更完整一些。

再来谈谈我一开始的思路

我一开始是想通过先做出t1表,然后再直接写个滑动窗口,就能做出类似于t2 那样的表,但是我发现无论我怎样写滑动窗口都会出现这样的结果↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ alt 包括我使用sum(if()) 来整合窗口函数,也会出现相同的结果,就是某个日期会出现两次,并且我统计一周前的有销售记录的商品也没有想出比较好的方法去统计,最后只能用比较极端的方法去解。

我在写通过的方法时也犯有一些错误

  1. 一开始没有想到限制店铺901的问题
  2. 其次也忘记了如果有别的店铺当天有销售记录也是要输出动销率和滞销率的,这个在写到后面的时候也给忘记了。
全部评论

相关推荐

1.自我介绍2.介绍一下mcp,&nbsp;skills3.了解react哪些状态管理库4.对话是sse还是什么?是用fetch还是EventSource?5.ts中的any&nbsp;和&nbsp;unknown讲一讲6.是直接用组件库的组件还是自己封装了一些别的7.代码输出题1function&nbsp;main()&nbsp;{{var&nbsp;a&nbsp;=&nbsp;1let&nbsp;b&nbsp;=&nbsp;2}console.log(a);console.log(b);}main()console.log(a);8.什么是块级作用域&nbsp;全局作用域&nbsp;函数作用域9.代码输出题2for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(i);},&nbsp;100);}10.代码输出题3for&nbsp;(var&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&amp;lt;&nbsp;5;&nbsp;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}11.代码输出题4for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{var&nbsp;temp&nbsp;=&nbsp;isetTimeout(()&nbsp;=&amp;gt;&nbsp;{console.log(temp);},&nbsp;100);}printText(i)}12.代码输出题5for(var&nbsp;i&nbsp;=&nbsp;0;i&nbsp;&amp;lt;&nbsp;5;i++){function&nbsp;printText(temp)&nbsp;{setTimeout(()&nbsp;=&amp;gt;&nbsp;{var&nbsp;temp&nbsp;=&nbsp;iconsole.log(temp);},&nbsp;100);}printText(i)}13.点击控制台输出题export&nbsp;default&nbsp;function&nbsp;App()&nbsp;{const&nbsp;[count,&nbsp;setCount]&nbsp;=&nbsp;useState(0)console.log('render',count)return&nbsp;(&lt;div&gt;&lt;h1&gt;{count}&lt;/h1&gt;{setCount(count&nbsp;+&nbsp;1)setTimeout(()&nbsp;=&amp;gt;&nbsp;console.log('setTimeout',&nbsp;count),&nbsp;1000)}}&amp;gt;+1&lt;/div&gt;)}//这个组件点击按钮后,控制台的输出顺序和值如下://&nbsp;1.&nbsp;render&nbsp;1&nbsp;(组件重新渲染,&nbsp;count&nbsp;更新为&nbsp;1)//&nbsp;2.&nbsp;setTimeout&nbsp;0&nbsp;(1秒后输出,注意这里是&nbsp;0&nbsp;而不是&nbsp;1)14.算法:给有序数组arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5],返回平方后的排序//&nbsp;有序数组平方后排序const&nbsp;arr&nbsp;=&nbsp;[-4,&nbsp;-1,&nbsp;0,&nbsp;3,&nbsp;5]function&nbsp;solution(arr)&nbsp;{const&nbsp;len&nbsp;=&nbsp;arr.lengthconst&nbsp;result&nbsp;=&nbsp;new&nbsp;Array(len)let&nbsp;left&nbsp;=&nbsp;0let&nbsp;right&nbsp;=&nbsp;len&nbsp;-&nbsp;1let&nbsp;index&nbsp;=&nbsp;len&nbsp;-&nbsp;1while&nbsp;(left&nbsp;&amp;lt;=&nbsp;right)&nbsp;{if&nbsp;(arr[left]&nbsp;*&nbsp;arr[left]&nbsp;&amp;gt;&nbsp;arr[right]&nbsp;*&nbsp;arr[right])&nbsp;{result[index]&nbsp;=&nbsp;arr[left]&nbsp;*&nbsp;arr[left]left++}&nbsp;else&nbsp;{result[index]&nbsp;=&nbsp;arr[right]&nbsp;*&nbsp;arr[right]right--}index--}return&nbsp;result}console.log(solution(arr));15.反问
查看14道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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