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

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

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

# 查询商品在24年上半年的总销量、销量最高月份的销售量、销量最低月份的销售量、平均月销售量

with
t1 as(
    select
        product_id,
        product_name,
        sale_month,
        sum(quantity) as monthly_sales,
        count(product_id)over(partition by product_id) as month_count
    from
        sales_underline left join products_underline using(product_id)
    where
        sale_month between '2024-01' and '2024-06'
    group by
        product_id,
        product_name,
        sale_month
)
,
t2 as(
    select distinct
        product_id,
        product_name,
        sum(monthly_sales)over(partition by product_name) as total_sales,
        max(monthly_sales)over(partition by product_name) as max_monthly_sales,
        min(monthly_sales)over(partition by product_name) as min_monthly_sales,
        month_count
    from
        t1
)

select
        product_id,
        product_name,
        total_sales,
        max_monthly_sales,
        min_monthly_sales,
        round(total_sales/month_count,0) as avg_monthly_sales
from
    t2
order by
    product_id

全部评论

相关推荐

小浪_Coding:1. 个人技能排版太乱, 写的技术栈太浅了, 跟测试,自动化相关的太少; 2. 项目开发类的太简单没有亮点, 算法类的项目建议只放一个,最好有自动化,CI/CD, pipline的项目, 需要更换; 3.整体排版需要优化, SOOB打招呼都需要注意等.
我的简历长这样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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