V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  Pandaaaa906  ›  全部回复第 1 页 / 共 1 页
回复总数  1
2022-11-19 10:34:30 +08:00
回复了 qiyong 创建的主题 程序员 面试中遇到的一道 sql 题
没限制数据库吧? mysql 太渣~用 postgres 了

select * from (
select
g.name,
t.sales_volume_sum,
gg.name,
sum(t.sales_volume_sum) over(partition by g.group_id) total_sum
from (
select
goods_id,
sum(gsr.sales_volume) sales_volume_sum
from goods_sales_record gsr
group by gsr.goods_id
) t
left join goods g
on t.goods_id = g.id

left join goods_group gg
on g.group_id = gg.id

order by t.sales_volume_sum desc
limit 3
) tmp
order by total_sum desc, sales_volume_sum desc
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   958 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 12ms · UTC 20:29 · PVG 04:29 · LAX 12:29 · JFK 15:29
Developed with CodeLauncher
♥ Do have faith in what you're doing.