1.創建測試表score
create table score( class_no varchar2(10), --班級 student_name varchar2(20), --姓名 score number --分數 );
2.初始化數據
insert into score(class_no,student_name,score) values('n001','park',99); insert into score(class_no,student_name,score) values('n001','ning',99); insert into score(class_no,student_name,score) values('n001','tom',79); insert into score(class_no,student_name,score) values('n001','cat',87); insert into score(class_no,student_name,score) values('n001','sandy',95); insert into score(class_no,student_name,score) values('n002','cake',85); insert into score(class_no,student_name,score) values('n002','mavom',69); insert into score(class_no,student_name,score) values('n002','tony',90); insert into score(class_no,student_name,score) values('n002','lisa',99); insert into score(class_no,student_name,score) values('n002','linda',67); insert into score(class_no,student_name,score) values('n003','versy',84); insert into score(class_no,student_name,score) values('n003','peter',97); insert into score(class_no,student_name,score) values('n003','train',83); insert into score(class_no,student_name,score) values('n003','rain',80);
3.將不同班級學生按分數降序排列
select * from (select class_no, student_name, score, rank() over(partition by class_no order by score desc) from score) t;
4.執行結果
5.其他分析函數
row_number() over(partition by ... order by ...) rank() over(partition by ... order by ...) dense_rank() over(partition by ... order by ...) count() over(partition by ... order by ...) max() over(partition by ... order by ...) min() over(partition by ... order by ...) sum() over(partition by ... order by ...) avg() over(partition by ... order by ...) first_value() over(partition by ... order by ...) last_value() over(partition by ... order by ...) lag() over(partition by ... order by ...) lead() over(partition by ... order by ...)