Rank()使用說明:
a. 函數簡介:
返回結果集分區內指定欄位的值的排名,指定欄位的值的排名是相關行之前的排名加一。
b. 文法:
RANK() OVER([<partiton_by_clause>]<order by clause>)
c. 參數說明:
partition_by_clause 將from子句產生的結果集劃分為應用到RANK函數的分區。
Order_by_clause確定將RANK值應用到分區中的行時所使用的順序。
d. 以下是執行個體使用:
1. 建立測試表
Sql代碼 --建立表 -- Create table create table T_SCORE ( AUTOID NUMBER not null, S_ID NUMBER(3), S_NAME CHAR(8) not null, SUB_NAME VARCHAR2(20), SCORE NUMBER(10,2) ); -- Add comments to the table comment on table T_SCORE is '學產生績表'; -- Add comments to the columns comment on column T_SCORE.AUTOID is '主鍵ID'; comment on column T_SCORE.S_ID is '學生ID'; comment on column T_SCORE.S_NAME is '學生姓名'; comment on column T_SCORE.SUB_NAME is '科目'; comment on column T_SCORE.SCORE is '成績';
2. 建立測試記錄
Sql代碼 insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (8, 1, '張三 ', '語文', 80.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (9, 2, '李四 ', '數學', 80.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (10, 1, '張三 ', '數學', 0.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (11, 2, '李四 ', '語文', 50.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (12, 3, '張三丰 ', '語文', 10.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (13, 3, '張三丰 ', '數學', null); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (14, 3, '張三丰 ', '體育', 120.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (15, 4, '楊過 ', 'JAVA', 90.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (16, 5, 'mike ', 'c++', 80.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (3, 3, '張三丰 ', 'Oracle', 0.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (4, 4, '楊過 ', 'Oracle', 77.00); insert into t_score (AUTOID, S_ID, S_NAME, SUB_NAME, SCORE) values (17, 2, '李四 ', 'Oracle', 77.00);
3. 分不同情況查詢
3.1 查詢所有的學產生績
Sql代碼 --1.查詢所有的學產生績