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 子句进行复杂查询。

相关推荐
蚊子不吸吸37 分钟前
DevOps开发运维简述
linux·运维·ci/cd·oracle·kubernetes·gitlab·devops
写bug的小屁孩2 小时前
登录注册窗口(一)
运维·服务器·数据库·c++·用户界面·qt6.3
q567315233 小时前
使用 Python 编辑 XML 文件中的文本字段
xml·java·数据库·python·sqlite
鲸屿1953 小时前
数据库第五次作业
数据库
陈大爷(有低保)3 小时前
数据库连接池JNDI
数据库·mysql·tomcat
Gauss松鼠会4 小时前
GaussDB的向量化处理技术
运维·数据库·database·gaussdb
Gauss松鼠会4 小时前
GaussDB Ustore存储引擎解读
java·前端·数据库·gaussdb
2401_857610035 小时前
Spring Boot技术在导师双选系统中的应用
java·数据库·spring boot
Ayka5 小时前
Linux Qt 6安装Oracle QOCI SQL Driver插件(适用WSL)
linux·sql·qt·oracle·oci
hummhumm6 小时前
Oracle 第19章:高级查询技术
java·数据库·python·sql·mysql·oracle·database