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

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

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. 过滤排名
全部评论

相关推荐

01-26 19:51
门头沟学院 Java
isabener:怎么感觉像群发的呢
点赞 评论 收藏
分享
King987:待优化的地方还是挺多的,可以参考一下我的作品优化一下,优化不好的话也可以找我
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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