---
title: "oracle聚合函数-----rank()和dense_rank()分类排名"
author: "小丑鱼0703"
date: "2012-10-17T17:20:14Z"
tags: []
categories: ["数据库"]
slug: "oracle-rank-dense-rank"
draft: false
lastmod: "2012-10-17T17:20:14Z"
id: 1700448
url: "https://wangjingyi.iteye.com/blog/1700448"
---

DENSE_RANK(n1...) WITHIN GROUP (ORDER BY col1    ]...) 计算指定值在记录集中的排序值。函数的参值必须一一对应group中的列，并且二者数据类型应该一致。至于order by子句中的nulls first|last则是用来设置记录集中值为null的列的排序在前或在后。
RANK() 参数及形式完全与上同，二区最大的区别是：RANK函数在处理指定数值在记录集中的排序值时，如果值有重复，则后面的排序值会跳过这个值，直接从当前排序值＋重复记录数开始，而DENSE_RANK则不会，排序值依然是个连续的序列。


```sql
select scott.emp.ename,scott.emp.deptno, scott.emp.sal,rank()over(partition by scott.emp.deptno order by scott.emp.sal) from scott.emp
```


![](http://dl.iteye.com/upload/attachment/0075/2102/57b99d0c-9c22-3502-9c49-ba616e327e18.png)


```sql
select scott.emp.ename,scott.emp.deptno, scott.emp.sal,dense_rank()over(partition by scott.emp.deptno order by scott.emp.sal) from scott.emp

```


![](http://dl.iteye.com/upload/attachment/0075/2112/c4003ca3-f2ce-3f53-9be2-e92fbb388492.png)