一题 MySQL 查询题求解

2021 年 4 月 5 日
 WaterWestBolus

销售表 sales_dtl 有城市(city),销售经理(cbm),客户(customer),销售金额(sale_amt)三列字段,请用 sql 取出每个城市中销售金额为本城市前 10 名的销售经理的信息。

是一题面试题,迫于 SQL 技术太差写不出答案。看了网上博客的资料,没看懂也没跑起来。故来求解

4989 次点击
所在节点    MySQL
20 条回复
acr0ss
2021 年 4 月 5 日
典型的 top k 题型,答题思路大致是两种。
1. 连表查询(一般解法)
2. 窗口函数( MySQL version >= 8.0 )

具体可以参考: [leetcode-cn ]( https://leetcode-cn.com/problems/department-top-three-salaries/comments/852822/),有**官方题解**。
Oktfolio
2021 年 4 月 5 日
select cbm, sum(sale_amt) as amt
from sales_dtl
group by cbm
order by amt desc
limit 10
Ptu2sha
2021 年 4 月 5 日
楼上果然不审题
Oktfolio
2021 年 4 月 5 日
噢,每个城市...
Ptu2sha
2021 年 4 月 5 日
substring_index(group_concat(cbm order by sale_amt),10) from sales_dtl group by city
大致这样写即可
WaterWestBolus
2021 年 4 月 5 日
@acr0ss thanks,看官方的有点累,我再研究一下。
WaterWestBolus
2021 年 4 月 5 日
@Ptu2sha 试了一下不太行。。我再看看。谢谢
flgn88
2021 年 4 月 5 日
window function 吧
Ptu2sha
2021 年 4 月 5 日
@WaterWestBolus 报错了 substring_index 少写了 , 、你查下文档
思路就是按城市分组 里面的销售额排序得到销售 然后取前十
acr0ss
2021 年 4 月 5 日
@WaterWestBolus 官网题解区现成的语句,还有中文解释。
shine20070522
2021 年 4 月 5 日
select t.city, substring_index(group_concat(t.cbm order by sale_amt desc),',',10)
from (
select city,cbm, sum(sale_amt) sale_amt from sales_dtl group by 1,2
) t
group by 1;
yungo8
2021 年 4 月 5 日
不是四列吗……
akira
2021 年 4 月 6 日
只会逐个城市取 然后代码合并一起。。。
xiaolanger
2021 年 4 月 6 日
这个得用上变量了
WaterWestBolus
2021 年 4 月 6 日
@shine20070522 非常感谢,成了!
raaaaaar
2021 年 4 月 6 日
select city, cbm, sale_amt
from sales_dtl as t1
where 10 > (
select count(*)
from sales_dtl as t2
where t1.city = t2.city
and t1.sale_amt < t2.sale_amt
);
raaaaaar
2021 年 4 月 6 日
大致想了下,重点是这个:
假设一共 n 个数,如果是从大往小排,那么排名第 m,就比这个数大的数的数目,就是 m-1,也就是说它前面又 m -1 个数,那么要找前 k 个数,只需要比这个数大的数目的个数小于 k 就行了,所以这里外面遍历确定一个数,然后里面找到比这个数大的数目小于 10 的就行了
akiraX
2021 年 4 月 6 日
组内自增序号辅助
select * from (
select a.*,
case when @pre_city=a.city then @n:=@n + 1 else @n:=1 end as i, # 判断城市是否切换,没切换累加序号,切换了从 1 开始
@pre_city := a.city # 记录当前城市到 pre_city,用于下一行判断,这一列必须在上面这列后面
from (
select city,cbm,sum(sale_amt) as total from sales_dtl group by city,cbm order by city,total desc # 分组求和,按城市、总金额倒序排序
) a

) b where b.i <= 10 # 取 top n
xxlee
2021 年 4 月 6 日
好奇 mysql 没有窗口函数 row_number over 么
zc832097
2021 年 4 月 6 日
@xxlee 8.0 才支持窗口函数 现在好多都还在用 5.x

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/768168

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX