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年'
	
相关推荐
datascome6 分钟前
文章发布易优CMS(Eyoucms)网站技巧
数据库·经验分享·爬虫·数据采集·eyoucms·易优cms
有想法的py工程师1 小时前
PostgreSQL 锁等待监控,查找等待中的锁
数据库
学不会就看1 小时前
Django--02模型和管理站点
数据库·oracle·django
←か淡定☆ ヾ2 小时前
SQL Server 2008R2 到 2012 数据库迁移完整指南
数据库·sql server
瀚高PG实验室2 小时前
Arcgis连接HGDB报错
数据库·arcgis·瀚高数据库
IT小辉同学3 小时前
PostgreSQL 与 MySQL 获取字段注释并转换为驼峰命名教程
数据库·mysql·postgresql
xinghunzhiye20103 小时前
redis升级
数据库·redis·缓存
一只fish3 小时前
MySQL 8.0 OCP 1Z0-908 题目解析(21)
数据库·mysql
涛思数据(TDengine)3 小时前
时序数据库 TDengine × SSRS:专为工业、能源场景打造的报表解决方案
大数据·数据库·物联网·时序数据库·tdengine
打鱼又晒网3 小时前
Lecture #20:Database Logging
数据库