网站首页 > 知识剖析 正文
背景
在数据分析中,经常会遇到按某某条件来排名、并找出排名的前几名,
用日常SQL的GROUP BY, ORDER BY来实现特别的麻烦,有时甚至实现不了,
这个时候SQL窗口函数就能发挥巨大作用了,窗口函数可以轻松解决下面的问题:
1)排名问题: 如按课程的学习成绩来排名
2) topN问题: 如找出每门课程成绩的前N名
什么是窗口函数
窗口函数,也叫OLAP函数(Online Anallytical Processing,联机分析处理),
可以对数据库数据进行实时分析处理;
这些处理通常很难通过聚合函数和分组操作来实现。
窗口函数定义
语法格式:
<窗口函数> over (partition by <用于分组的列名> order by <用于排序的列名>)
窗口函数的功能:
1) 同时具有分组和排序的功能
2) 不减少原有的行数
3) 窗口表示范围
窗口函数分类
1) 专用窗口函数:
rank() : 计算排名,存在并列排名,并列占位置
dense_rank():计算排名,存在并列排名,并列不占位置
row_number():计算排名,不存在并列
2) 聚合函数:
sum():求和
avg():求平均值
count():计算总记录数
max():求最大值
min():求最小值
演示
学生成绩表t_stu_score:
学号 (sid) | 课程编号 (cid) | 成绩 (score) |
s01 | c01 | 88 |
s02 | c01 | 98 |
s03 | c01 | 90 |
s01 | c02 | 89 |
s02 | c02 | 90 |
s03 | c02 | 90 |
-- 建表
create table t_stu_score(
sid varchar(10),
cid varchar(10),
score int
)
-- 添加测试数据
insert into t_stu_score values('s01', 'c01', 88);
insert into t_stu_score values('s02', 'c01', 98);
insert into t_stu_score values('s03', 'c01', 90);
insert into t_stu_score values('s01', 'c02', 89);
insert into t_stu_score values('s02', 'c02', 90);
insert into t_stu_score values('s03', 'c02', 90);
1.按课程的学习成绩来排名查询
select *,
rank() over(partition by cid order by score desc) as ranking,
dense_rank() over(partition by cid order by score desc) as dense_ranking,
row_number() over(partition by cid order by score desc) as row_num,
avg(score) over(partition by cid) as avg_score,
max(score) over(partition by cid) as max_score,
min(score) over(partition by cid) as min_score
from t_stu_score
结果:
2.找出每门课程成绩的前两名
select * from (
select *,
rank() over(partition by cid order by score desc) as ranking,
dense_rank() over(partition by cid order by score desc) as dense_ranking,
row_number() over(partition by cid order by score desc) as row_num,
avg(score) over(partition by cid) as avg_score,
max(score) over(partition by cid) as max_score,
min(score) over(partition by cid) as min_score
from t_stu_score
) a where a.ranking <=2
结果:
猜你喜欢
- 2025-09-06 postgresql自定义函数实现,通过contrib模块进行扩展
- 2025-09-06 PostgreSQL "+"操作符函数的获取机制
- 2025-09-06 VBA中SQLOpen函数_vba调用sql获取数据
- 2025-09-06 VBA中SQLExecQuery函数_vba操作sql数据库
- 2025-09-06 第 10 章:聚合函数与分组 - PostgreSQL入门
- 2025-09-06 SQL面试经典问题-开窗函数_sql开窗函数详解
- 2025-09-06 技术栈:我们把SQL窗口函数分为5大类12小类,这样好记吗?
- 2025-09-06 第 15 章:窗口函数 (Window Functions) - PostgreSQL入门
- 2025-09-06 SQL常用经典函数大全_sql函数有哪些
- 2025-09-06 SQL窗口函数知多少?_sql窗口函数有哪些
- 最近发表
-
- postgresql自定义函数实现,通过contrib模块进行扩展
- PostgreSQL "+"操作符函数的获取机制
- VBA中SQLOpen函数_vba调用sql获取数据
- VBA中SQLExecQuery函数_vba操作sql数据库
- 第 10 章:聚合函数与分组 - PostgreSQL入门
- SQL面试经典问题-开窗函数_sql开窗函数详解
- 技术栈:我们把SQL窗口函数分为5大类12小类,这样好记吗?
- 第 15 章:窗口函数 (Window Functions) - PostgreSQL入门
- SQL常用经典函数大全_sql函数有哪些
- SQL窗口函数知多少?_sql窗口函数有哪些
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)