如何在 Oracle 中为查询结果分配排名?
问题陈述
您希望分配一个表示它们在结果中位置的数字/排名。
解决方案
Oracle 提供了 **RANK** 分析函数来为结果集中的行生成排名号。为了演示,我们将按费用对学生进行排名,从最高付费到最低付费。以下 SELECT 语句使用 rank 函数来分配这些值。
示例
SELECT student_id, first_name, last_name, fees, RANK() OVER (ORDER BY fees DESC) AS rnk FROM students;
输出
学生ID | 名字 | 姓氏 | 费用 | 排名 |
100 | SMITH | JAMES | 24000 | 1 |
101 | JOHNSON | JOHN | 17000 | 2 |
102 | WILLIAMS | ROBERT | 17000 | 2 |
108 | RODRIGUEZ | JOSEPH | 12008 | 4 |
103 | BROWN | MICHAEL | 9000 | 5 |
109 | WILSON | THOMAS | 9000 | 5 |
110 | MARTINEZ | CHRISTOPHER | 8200 | 7 |
112 | TAYLOR | PAUL | 7800 | 8 |
111 | ANDERSON | DANIEL | 7700 | 9 |
113 | THOMAS | MARK | 6900 | 10 |
104 | JONES | WILLIAM | 6000 | 11 |
105 | MILLER | DAVID | 4800 | 12 |
106 | DAVIS | RICHARD | 4800 | 12 |
107 | GARCIA | CHARLES | 4200 | 14 |
RANK 的行为类似于任何其他分析函数,在非分析处理完成后,对结果集进行第二次传递。在上面的 SQL 中,结果按费用降序处理,并从 1 开始计算结果上的排名值。
几个费用相同的学生(12008)的排名为 4。这被称为稀疏排名,其中相同的值具有相同的排名。根据您的需求,这可能是或可能不是期望的结果,因为缺少一些排名。
稀疏排名的替代方案称为密集排名。我们将观察使用 dense_rank 函数的结果。
示例
SELECT student_id, first_name, last_name, fees, dense_rank() OVER (ORDER BY fees DESC) AS rnk FROM students
输出
学生ID | 名字 | 姓氏 | 费用 | 排名 |
100 | SMITH | JAMES | 24000 | 1 |
101 | JOHNSON | JOHN | 17000 | 2 |
102 | WILLIAMS | ROBERT | 17000 | 2 |
108 | RODRIGUEZ | JOSEPH | 12008 | 3 |
103 | BROWN | MICHAEL | 9000 | 4 |
109 | WILSON | THOMAS | 9000 | 4 |
110 | MARTINEZ | CHRISTOPHER | 8200 | 5 |
112 | TAYLOR | PAUL | 7800 | 6 |
111 | ANDERSON | DANIEL | 7700 | 7 |
113 | THOMAS | MARK | 6900 | 8 |
104 | JONES | WILLIAM | 6000 | 9 |
105 | MILLER | DAVID | 4800 | 10 |
106 | DAVIS | RICHARD | 4800 | 10 |
107 | GARCIA | CHARLES | 4200 | 11 |
现在我们可以看到缺少的连续排名值。
数据准备:用于问题的的数据如下所示。
示例
DROP TABLE students; COMMIT; CREATE TABLE students ( student_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) , email VARCHAR2(40) , phone_number VARCHAR2(20) , join_date DATE , class_id VARCHAR2(20) , fees NUMBER(8,2) , professor_id NUMBER(6) , department_id NUMBER(4) ) ;
示例
CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ; INSERT INTO students VALUES (100,'SMITH','JAMES','[email protected]','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','[email protected]','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','[email protected]','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','[email protected]','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','[email protected]','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','[email protected]','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','[email protected]','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','[email protected]','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','[email protected]','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','[email protected]','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','[email protected]','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','[email protected]','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','[email protected]','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','[email protected]','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;
广告