Oracle 中排序函数总结

一、rownum

rownum为最简单的序号 但是在order by之前就确定值。

cpp 复制代码
select
	rownum,--序号
	year_name,--年份名称
	month_name,--月份名称
	post_code,--岗位编码
	post_name,--岗位名称
	testtype_code,--试验类型编码
	testtype_name,--试验类型名称
	cost_amt--产值
	from test_2021
where 
	year_name = '2016年' and month_name = '12月'
order by cost_amt

二、row_number

2.1、row_number() over( order by 字段名1,字段名2,...字段名n )

先排序,再确定序号

cpp 复制代码
select
	row_number() over( order by  cost_amt)  as xuhao,--序号
	year_name,--年份名称
	month_name,--月份名称
	post_code,--岗位编码
	post_name,--岗位名称
	testtype_code,--试验类型编码
	testtype_name,--试验类型名称
	cost_amt--产值
from 
	test_2021 a
where 
	year_name = '2016年'

2.2、row_number() over(partition by 字段名1,字段名2,...字段名n order by 字段名1,字段名2,...字段名n )

先排序再确定序号,会根据 partition 分区,在每一个小分区内部取序号

cpp 复制代码
select
	row_number() over(partition by month_name order by  cost_amt)  as xuhao,--序号
	year_name,--年份名称
	month_name,--月份名称
	post_code,--岗位编码
	post_name,--岗位名称
	testtype_code,--试验类型编码
	testtype_name,--试验类型名称
	cost_amt--产值
from 
	test_2021 a
where 
	year_name = '2016年'
	
-- 根据 月份名称 进行分区,在分区内在进行排序,生成序号。

三、rank()

rank()和row_number() 函数用法类似,但是rank()生成的序号是同值同序的不连续序号,即如果出现相同的值,那么序号是一样的。

cpp 复制代码
select
	rank() over(order by  cost_amt)  as xuhao,--序号
	year_name,--年份名称
	month_name,--月份名称
	post_code,--岗位编码
	post_name,--岗位名称
	testtype_code,--试验类型编码
	testtype_name,--试验类型名称
	cost_amt--产值
from 
	test_2021 a
where
	 year_name = '2016年'
	 

四、dense_rank()

dense_rank()与rank()的区别在于,dense_rank()生成的序号是连续的。

cpp 复制代码
select
	dense_rank() over(order by  cost_amt)  as xuhao,--序号
	year_name,--年份名称
	month_name,--月份名称
	post_code,--岗位编码
	post_name,--岗位名称
	testtype_code,--试验类型编码
	testtype_name,--试验类型名称
	cost_amt--产值
from 
	test_2021 a
where 
	year_name = '2016年'
	
相关推荐
百结2141 小时前
Mysql数据库操作
数据库·mysql·oracle
keep one's resolveY1 小时前
时区问题解决
数据库
Leinwin1 小时前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
qq_417695052 小时前
机器学习与人工智能
jvm·数据库·python
漫随流水2 小时前
旅游推荐系统(view.py)
前端·数据库·python·旅游
ego.iblacat2 小时前
MySQL 服务基础
数据库·mysql
Maverick063 小时前
Oracle Redo 日志操作手册
数据库·oracle
攒了一袋星辰4 小时前
高并发强一致性顺序号生成系统 -- SequenceGenerator
java·数据库·mysql
W.D.小糊涂4 小时前
gpu服务器安装windows+ubuntu24.04双系统
c语言·开发语言·数据库
云贝教育-郑老师4 小时前
【OceanBase 的多租户架构是怎样的?有什么优势?】
数据库·oceanbase