需求查出每个人的最高分数的那条数据
| id | name | score |
|---|---|---|
| 1 | aa | 14 |
| 2 | aa | 13 |
有三种思路
一:any_value函数(不符合要求)
sql
select any_value(id),name,any_value(score) from table
group by name
无法取出分数最高的那条数据
二:max聚合函数(不符合要求)
sql
select max(id),name,max(score) from table
group by name
查出来的数据应该是
| id | name | score |
|---|---|---|
| 2 | aa | 14 |
这已经不是原有的数据了
三:窗口函数
这么说来 窗口函数更适合控制自己最想要的数据
sql
select o.* from (select i.*, rank() over (partition by name order by score) as ranking from
(select id,name,score from table) i) o where ranking = 1

评论区 (0 条评论)
暂无评论,来发表第一条评论吧!