






















🪄 一、窗口函数简介
窗口函数(Window Function)用于对查询结果集中的每一行计算“相对排名”或“顺序编号”。
三大常用函数:
| 函数 | 功能描述 | 相同值处理 | 是否跳号 | 示例序号 |
|---|---|---|---|---|
CREATE TABLE scores (
student_name VARCHAR(10),
score INT
);
INSERT INTO scores VALUES
('张三', 95),
('李四', 92),
('王五', 92),
('赵六', 88),
('孙七', 85);
SELECT
student_name,
score,
ROW_NUMBER() OVER (ORDER BY score DESC) AS rn,
RANK() OVER (ORDER BY score DESC) AS rk,
DENSE_RANK() OVER (ORDER BY score DESC) AS dr
FROM scores;
| student_name | score | ROW_NUMBER | RANK | DENSE_RANK |
|---|---|---|---|---|
| 函数 | 并列情况 | 下一名 | 解释 |
|---|---|---|---|
有些旧版本数据库(如 MySQL 5.x)不支持窗口函数,我们可以通过自联结 (Self Join) 实现相同逻辑。
思路:
计算比当前行分数更高的行数(包括当前行自己)
→ 直接用 >= 替代 > + 1。
SELECT
a.student_name,
a.score,
COUNT(b.student_name) AS rank
FROM scores a
LEFT JOIN scores b
ON b.score >= a.score
GROUP BY a.student_name, a.score
ORDER BY a.score DESC;
📈 结果:
| student_name | score | rank |
|---|---|---|
✅ 等价于 RANK() OVER (ORDER BY score DESC)。
思路:
统计“比当前行分数高的不同分数”数量,再加上自己。
SELECT
a.student_name,
a.score,
COUNT(DISTINCT b.score) AS dense_rank
FROM scores a
LEFT JOIN scores b
ON b.score >= a.score
GROUP BY a.student_name, a.score
ORDER BY a.score DESC;
📈 结果:
| student_name | score | dense_rank |
|---|---|---|
✅ 等价于 DENSE_RANK() OVER (ORDER BY score DESC)。
思路:
ROW_NUMBER() 必须每行唯一编号,不能并列。
所以在分数相同的情况下,还需要用另一个唯一字段打破并列(如 student_name 或 id)。
SELECT
a.student_name,
a.score,
COUNT(b.student_name) AS row_number
FROM scores a
LEFT JOIN scores b
ON b.score > a.score
OR (b.score = a.score AND b.student_name < a.student_name)
GROUP BY a.student_name, a.score
ORDER BY a.score DESC, a.student_name;
📈 结果:
| student_name | score | row_number |
|---|---|---|
✅ 等价于 ROW_NUMBER() OVER (ORDER BY score DESC, student_name ASC)。
PARTITION BY用于在窗口函数中进行分组计算,类似于 GROUP BY的概念,但关键区别是:它不会合并行,而是在每个分组内独立进行计算。
函数名() OVER (
PARTITION BY 分组字段
ORDER BY 排序字段
)
假设我们有一个更详细的学生成绩表:
| student_id | student_name | class_name | score |
|---|---|---|---|
SELECT
student_id,
student_name,
class_name,
score,
ROW_NUMBER() OVER (PARTITION BY class_name ORDER BY score DESC) AS rn,
RANK() OVER (PARTITION BY class_name ORDER BY score DESC) AS rk,
DENSE_RANK() OVER (PARTITION BY class_name ORDER BY score DESC) AS dr
FROM students;
| student_id | student_name | class_name | score | rn | rk | dr |
|---|---|---|---|---|---|---|
ROW_NUMBER():强制分配2和3(不确定顺序) RANK():都排名第2,下一个吴九排名第4(跳过第3名) DENSE_RANK():都排名第2,下一个吴九排名第3(连续) SELECT * FROM (
SELECT
student_name,
class_name,
score,
ROW_NUMBER() OVER (PARTITION BY class_name ORDER BY score DESC) as rank_in_class
FROM students
) AS ranked_students
WHERE rank_in_class <= 2;
SELECT
student_name,
class_name,
score,
ROUND(
PERCENT_RANK() OVER (PARTITION BY class_name ORDER BY score) * 100, 2
) as percentile_in_class
FROM students;
SELECT
student_name,
class_name,
score,
-- 班级内排名
RANK() OVER (PARTITION BY class_name ORDER BY score DESC) as class_rank,
-- 班级内平均分
AVG(score) OVER (PARTITION BY class_name) as class_avg,
-- 班级内最高分
MAX(score) OVER (PARTITION BY class_name) as class_max
FROM students;
PARTITION BY class_name, subject_name PARTITION BY它实现了"既要分组计算,又要保留明细数据"的需求。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。