唯一麻烦的就是按年龄占比,采用SUM()OVER

对商品的销售情况进行深度分析

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

  • sum(sum(sa.quantity * sa.price)) over(partition by pd.category)
  • 是一个窗口函数,它对每个 pd.category 分组内的 sum(sa.quantity * sa.price) 的结果再次求和。
  • 由于 over(partition by pd.category) 的存在,这个窗口函数会为每个分组内的每一行返回该分组的 sum(sa.quantity * sa.price) 的总和。
select
    pd.category         as product_category,
    ci.age_group        as age_group,
    sum(quantity*price) as total_sales_amount,
    round(
    sum(	sa.quantity * sa.price)/
    sum(sum(sa.quantity * sa.price))over(partition by pd.category),2
        )               as purchase_percentage
from
    products            as pd
    join sales          as sa on sa.product_id = pd.product_id
    join customer_info  as ci on ci.sale_id    = sa.sale_id

group by 
    pd.category,ci.age_group
order by 
    product_category,purchase_percentage desc

全部评论

相关推荐

牛客50327486...:腾讯官方:我们没有人机对局
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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