题解 | 电商平台想要了解不同商品在不同月份的销售趋势

电商平台想要了解不同商品在不同月份的销售趋势

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

#开始我想使用and year(s.sale_month)=2023 and month(s.sale_month)<7作为条件筛选的,但是函数的返回结果一直是NULL,添加了str_to_date(s.sale_month, "%Y-%m")之后还是不行,也不知道为什么,就直接看讨论里面的用between...and了

with window_tb as (
    select
        p.product_id,
        quantity,
        row_number() over(partition by product_name order by quantity desc) as max_ranking,
        row_number() over(partition by product_name order by quantity) as min_ranking
    from
        products_underline as p
        left join sales_underline as s on p.product_id=s.product_id and sale_month between "2024-01" and "2024-06"
)

select
    product_id,
    product_name,
    (
        select sum(quantity) 
        from sales_underline as s
        where s.product_id=p.product_id and sale_month between "2024-01" and "2024-06"
    ) as total_sales,
    (
        select quantity
        from window_tb
        where window_tb.product_id=p.product_id and window_tb.max_ranking=1
    ) as max_monthly_sales,
    (
        select quantity
        from window_tb
        where window_tb.product_id=p.product_id and window_tb.min_ranking=1
    ) as min_monthly_sales,
    (
        select convert(avg(quantity), signed) 
        from sales_underline as s
        where s.product_id=p.product_id and sale_month between "2024-01" and "2024-06"
    ) as avg_monthly_sales
from products_underline as p
order by product_id

全部评论

相关推荐

10-26 13:03
已编辑
门头沟学院 物流经理
在okr拆解的小太阳...:实习待过,我待的部门氛围很好,基本不加班,双休有保证。
投递正浩创新EcoFlow等公司10个岗位
点赞 评论 收藏
分享
11-19 18:44
已编辑
成都理工大学 Java
程序员花海:我面试过100+校招生,大厂后端面试不看ACM,竞赛经历含金量低于你有几份大厂实习 这个简历整体来看不错 可以海投
如何写一份好简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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