题解 | 统计每个产品的销售情况

统计每个产品的销售情况

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

with t1 as (
select o.product_id,
sum(quantity) *p.unit_price total_sales,
unit_price,sum(quantity) total_quantity,
round(sum(quantity) *p.unit_price/12,2) avg_monthly_sales
# row_number() over(partition by p.product_id order by sum(quantity) desc) rn
from orders o
join products p on o.product_id = p.product_id
group by o.product_id
)
,t2 as (-- 各产品的各月销量及其次序表
select o.product_id,
# month(order_date) month,
sum(quantity) monthly_quantity,
row_number() over(partition by o.product_id order by sum(quantity) desc) rn
from orders o 
join products p on o.product_id = p.product_id
group by o.product_id,month(order_date)
order by o.product_id,month(order_date)
)
,t3 as (-- 各产品的消费者购买量次序表
select o.product_id,
# o.customer_id,
customer_age,
# sum(quantity),
row_number() over(partition by o.product_id order by sum(quantity) desc,customer_age) crn
from orders o
join customers c on o.customer_id = c.customer_id
group by o.product_id,o.customer_id
order by o.product_id,o.customer_id
)

select t1.product_id,
total_sales,
unit_price,
total_quantity,
avg_monthly_sales,
t2.monthly_quantity max_monthly_quantity,
case
when t3.customer_age>=1 and t3.customer_age<=10 then '1-10'
when t3.customer_age>=11 and t3.customer_age<=20 then '11-20'
when t3.customer_age>=21 and t3.customer_age<=30 then '21-30'
when t3.customer_age>=31 and t3.customer_age<=40 then '31-40'
when t3.customer_age>=41 and t3.customer_age<=50 then '41-50'
when t3.customer_age>=51 and t3.customer_age<=60 then '51-60'
else '61+'
end as customer_age_group
from t1
join t2 on t1.product_id = t2.product_id
join t3 on t1.product_id = t3.product_id
where t2.rn =1 and t3.crn =1
order by total_sales desc,
product_id asc

全部评论

相关推荐

01-12 22:27
武汉大学 Java
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
2025-12-17 16:48
今天九点半到公司,我跟往常一样先扫了眼电脑,屁活儿没有。寻思着没事干,就去蹲了个厕所,回来摸出手机刷了会儿。结果老板刚好路过,拍了我一下说上班别玩手机,我吓得赶紧揣兜里。也就过了四十分钟吧,我的直属领导把我叫到小隔间,上来就给我一句:“你玩手机这事儿把老板惹毛了,说白了,你可以重新找工作了,等下&nbsp;HR&nbsp;会来跟你谈。”&nbsp;我当时脑子直接宕机,一句话都没憋出来。后面&nbsp;HR&nbsp;找我谈话,直属领导也在旁边。HR&nbsp;说我这毛病不是一次两次了,属于屡教不改,不光上班玩手机,还用公司电脑看论文、弄学校的事儿。我当时人都傻了,上班摸鱼是不对,可我都是闲得发慌的时候才摸啊!而且玩手机这事儿,从来没人跟我说过后果这么严重,更没人告诉我在公司学个习也算犯错!连一次口头提醒都没有,哪儿来的屡教不改啊?更让我膈应的是,昨天部门刚开了会,说四个实习生里留一个转正,让大家好好表现。结果今天我就因为玩手机被开了。但搞笑的是,开会前直属领导就把我叫去小会议室,明明白白告诉我:“转正这事儿你就别想了,你的学历达不到我们部门要求,当初招你进来也没打算给你这个机会。”合着我没入贵厂的眼是吧?可我都已经被排除在转正名单外了,摸个鱼至于直接把我开了吗?真的太离谱了!
rush$0522:转正名单没进,大概率本来就没打算留你
摸鱼被leader发现了...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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