题解 | 最畅销的SKU

最畅销的SKU

https://www.nowcoder.com/practice/356a64a402864b27a9ab47d0c032756d

with a as(
    select 
        ss.store_id,
        store_name,
        city,
        ss.sku_id,
        ifnull(sum(qty),0) as last7d_qty,
        ifnull(round(sum(qty)/7,2),0.00) as avg_daily_qty,
        ss.stock_qty,
        CASE WHEN round(sum(qty)/7,2)>0 THEN ROUND(ss.stock_qty/round(sum(qty)/7,2), 1) ELSE NULL END as coverage_days,
        ROW_NUMBER() over(partition by store_id order by sum(qty) DESC,sku_id ) as rank_in_store
    from store_stock_ ss 
        left join store_info_ si using(store_id)
        left join (
            select  sales_daily_.*
            from sales_daily_  join store_stock_ using(store_id,sku_id )
            where sale_date between subdate(snapshot_date,6) and snapshot_date 
            ) sd using(store_id,sku_id )
    
    group by ss.store_id,ss.sku_id,store_name,city,ss.stock_qty

)
select 
    store_id,
    store_name,
    city,
    sku_id,
    last7d_qty,
    avg_daily_qty,
    stock_qty,
    coverage_days,
    rank_in_store
from a
where rank_in_store<=3;

全部评论

相关推荐

01-15 19:59
中山大学 C++
牛客60887332...:你这是人写出来的? 本科标到硕士后面 留那么多空给 hr 填?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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