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年'
	
相关推荐
呆呆小金人3 分钟前
SQL入门:正则表达式-高效文本匹配全攻略
大数据·数据库·数据仓库·sql·数据库开发·etl·etl工程师
白鲸开源1 小时前
(二)从分层架构到数据湖仓架构:数据仓库分层下的技术架构与举例
大数据·数据库·数据分析
好玩的Matlab(NCEPU)1 小时前
Redis vs RabbitMQ 对比总结
数据库·redis·rabbitmq
21号 11 小时前
16.MySQL 服务器配置与管理
服务器·数据库·mysql
我的offer在哪里1 小时前
MongoDB
数据库·mongodb
练习时长一年3 小时前
AI开发结构化输出
数据库
IvorySQL3 小时前
灾难恢复工具内核细节探究与分享
数据库·postgresql·开源
lypzcgf3 小时前
商城小程序数据库表结构文档
数据库·小程序·电商
jjw_zyfx4 小时前
Ubuntu上vue3 vite使用MBTiles搭建地图服务器
服务器·数据库·ubuntu
EndingCoder4 小时前
Node.js SQL数据库:MySQL/PostgreSQL集成
javascript·数据库·sql·mysql·postgresql·node.js