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年'
	
相关推荐
持敬chijing10 分钟前
Web渗透之SQL注入-SQLMAP使用笔记
数据库·sql·安全·web安全·网络安全·网络攻击模型
瀚高PG实验室11 分钟前
流复制备库停机维护前检查步骤
数据库·瀚高数据库·highgo
BomanGe221 分钟前
NSK直线导轨LH55EL与NH55EM替代指南
前端·javascript·数据库·经验分享·规格说明书
JAVA面经实录91722 分钟前
MongoDB(文档型 NoSQL)
java·数据库·mongodb·nosql
睡不醒男孩03082324 分钟前
第十篇:PostgreSQL 生产环境高可用选型:CLUP 与 Patroni 深度架构对比与踩坑实录
数据库·postgresql·架构
JAVA面经实录91727 分钟前
HBase 知识点梳理(文档型 NoSQL)
大数据·数据库·nosql数据库·hbase
小二·34 分钟前
PostgreSQL 高级特性与性能调优
数据库·postgresql
风味蘑菇干1 小时前
JDBC(数据库连接池&DBUtils)
java·数据库
标书畅畅行1 小时前
深度解析钛投标AI标书工具:全流程企业级AI投标解决方案,重构投标数字化生产力
大数据·数据库·人工智能
Wait....1 小时前
MySQL底层知识总结
数据库·mysql