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年'
	
相关推荐
IvorySQL13 分钟前
PostgreSQL 分区表的 ALTER TABLE 语句执行机制解析
数据库·postgresql·开源
·云扬·23 分钟前
MySQL 8.0 Redo Log 归档与禁用实战指南
android·数据库·mysql
IT邦德26 分钟前
Oracle 26ai DataGuard 搭建(RAC到单机)
数据库·oracle
惊讶的猫1 小时前
redis分片集群
数据库·redis·缓存·分片集群·海量数据存储·高并发写
不爱缺氧i1 小时前
完全卸载MariaDB
数据库·mariadb
纤纡.1 小时前
Linux中SQL 从基础到进阶:五大分类详解与表结构操作(ALTER/DROP)全攻略
linux·数据库·sql
jiunian_cn1 小时前
【Redis】渐进式遍历
数据库·redis·缓存
橙露2 小时前
Spring Boot 核心原理:自动配置机制与自定义 Starter 开发
java·数据库·spring boot
冰暮流星2 小时前
sql语言之分组语句group by
java·数据库·sql
符哥20082 小时前
Ubuntu 常用指令集大全(附实操实例)
数据库·ubuntu·postgresql