题解 | 哪些产品在特定时间段内表现最为出色

哪些产品在特定时间段内表现最为出色

https://www.nowcoder.com/practice/866a4614615b43a29750537ede4bf0c8

with
    tt as (
        select
            p.product_id product_id,
            p.product_name product_name,
            sum(s.sales_amount) total_sales_amount,
            sum(s.sales_quantity) total_sales_quantity
        from
            products p
            join sales_records s on p.product_id = s.product_id
        where
            (
                s.sales_date >= '2024-01-01'
                and s.sales_date <= '2024-12-31'
            )
        group by
            1,
            2
    ),
    tt_with_rk as (
        select
            product_id,
            product_name,
            total_sales_amount,
            total_sales_quantity,
            rank() over (
                order by
                    total_sales_quantity desc
            ) as rk
        from
            tt
    )
select
    product_id,
    product_name,
    total_sales_amount,
    total_sales_quantity
from
    tt_with_rk
where
    rk = 1

  1. 计算总销售额
  2. rank over排名
  3. 过滤排名
全部评论

相关推荐

2025-12-27 22:01
北京邮电大学 Java
蛊界Go学长林剑行:北邮加粗放大
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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