Oracle 开窗函数

在 Oracle 数据库中,开窗函数(Window Functions)是一类非常强大的 SQL 函数,用于在结果集的"窗口"上执行计算。这些窗口函数允许你在查询的每一行上执行聚合操作(如求和、平均、计数等),同时保留原始行的详细数据。

以下是一些常见的 Oracle 开窗函数及其用法:

1. ROW_NUMBER()

为结果集中的每一行分配一个唯一的序号。

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank_in_dept 
FROM 
employees;

2. RANK()

类似于 ROW_NUMBER(),但如果有相同的值,它们会共享相同的排名,并且后续排名会跳过。

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank_in_dept 
FROM 
employees;

3. DENSE_RANK()

类似于 RANK(),但后续排名不会跳过。

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS dense_rank_in_dept 
FROM 
employees;

4. NTILE(n)

将结果集划分为 n 个桶,并为每一行分配一个桶号。

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
NTILE(4) OVER (PARTITION BY department_id ORDER BY salary DESC) AS quartile 
FROM 
employees;

5. LAG() 和 LEAD()

用于访问结果集中当前行的前一行或后一行的数据。

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
LAG(salary, 1) OVER (PARTITION BY department_id ORDER BY salary) AS prev_salary, 
LEAD(salary, 1) OVER (PARTITION BY department_id ORDER BY salary) AS next_salary 
FROM 
employees;

6. FIRST_VALUE() 和 LAST_VALUE()

返回窗口框架中的第一行或最后一行的值。

sql 复制代码
SELECT

employee_id,

department_id,

salary,

FIRST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS first_salary_in_dept,

LAST_VALUE(salary) OVER (PARTITION BY department_id ORDER BY salary RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_salary_in_dept

FROM

employees;

7. SUM(), AVG(), MIN(), MAX() 等聚合函数

在窗口上执行常见的聚合操作。

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
SUM(salary) OVER (PARTITION BY department_id ORDER BY salary RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_salary 
FROM 
employees;

窗口子句(Window Clause)

在开窗函数中,OVER 子句用于定义窗口的分区和排序。PARTITION BY 子句用于将结果集划分为分区,ORDER BY 子句用于定义每个分区内的排序顺序。

sql 复制代码
OVER ( 
[PARTITION BY partition_expression, ...] 
[ORDER BY sort_expression [ASC|DESC], ...] 
[ROWS|RANGE BETWEEN frame_start AND frame_end] 
)
  • PARTITION BY:将结果集划分为多个分区,每个分区独立计算。
  • ORDER BY:定义每个分区内的排序顺序。
  • ROWSRANGE:定义窗口框架(frame),即窗口内包含的行范围。

示例:综合使用

sql 复制代码
SELECT 
employee_id, 
department_id, 
salary, 
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank_in_dept, 
SUM(salary) OVER (PARTITION BY department_id ORDER BY salary RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cumulative_salary 
FROM 
employees;

以上示例展示了如何使用多个开窗函数,并结合 PARTITION BYORDER BY 子句进行复杂查询。

相关推荐
Three~stone29 分钟前
MySQL学习集--DDL
数据库·sql·学习
Qi妙代码31 分钟前
MYSQL基础
数据库·mysql·oracle
Arbori_2621533 分钟前
Oracle中的UNION原理
数据库·oracle
llzcxdb36 分钟前
【MySQL】理解MySQL的双重缓冲机制:Buffer Pool与Redo Log的协同之道
数据库·mysql
SelectDB技术团队1 小时前
Apache Doris 2025 Roadmap:构建 GenAI 时代实时高效统一的数据底座
大数据·数据库·数据仓库·人工智能·ai·数据分析·湖仓一体
冷凝女子1 小时前
【QT】获取文件路径中的文件名,去掉后缀,然后提取文件名中的数字
开发语言·数据库·qt
Y第五个季节1 小时前
Redis - HyperLogLog
数据库·redis·缓存
Allen Bright2 小时前
【MySQL基础-20】MySQL条件函数全面解析:提升查询逻辑的利器
数据库·mysql
Justice link2 小时前
企业级NoSql数据库Redis集群
数据库·redis·缓存
爱的叹息2 小时前
主流数据库的存储引擎/存储机制的详细对比分析,涵盖关系型数据库、NoSQL数据库和分布式数据库
数据库·分布式·nosql