S_ID
|
S_NAME
|
SUB_NAME
|
SCORE
|
1
|
張三
|
語文
|
80.00
|
2
|
李四
|
數(shù)學
|
80.00
|
1
|
張三
|
數(shù)學
|
0.00
|
2
|
李四
|
語文
|
50.00
|
3
|
張三豐
|
語文
|
10.00
|
3
|
張三豐
|
數(shù)學
|
|
3
|
張三豐
|
體育
|
120.00
|
4
|
楊過
|
JAVA
|
90.00
|
5
|
mike
|
c++
|
80.00
|
3
|
張三豐
|
Oracle
|
0.00
|
4
|
楊過
|
Oracle
|
77.00
|
2
|
李四
|
Oracle
|
77.00
|
S_ID
|
S_NAME
|
SUB_NAME
|
SCORE
|
名次
|
4
|
楊過
|
Oracle
|
77.00
|
1
|
2
|
李四
|
Oracle
|
77.00
|
1
|
3
|
張三豐
|
Oracle
|
0.00
|
3
|
S_ID
|
S_NAME
|
SUB_NAME
|
SCORE
|
名次
|
4
|
楊過
|
Oracle
|
77.00
|
1
|
2
|
李四
|
Oracle
|
77.00
|
1
|
3
|
張三豐
|
Oracle
|
0.00
|
2
|
S_ID
|
S_NAME
|
SUB_NAME
|
SCORE
|
名次
|
4
|
楊過
|
JAVA
|
90.00
|
1
|
4
|
楊過
|
Oracle
|
77.00
|
1
|
2
|
李四
|
Oracle
|
77.00
|
1
|
3
|
張三豐
|
Oracle
|
0.00
|
3
|
5
|
mike
|
c++
|
80.00
|
1
|
3
|
張三豐
|
數(shù)學
|
|
1
|
2
|
李四
|
數(shù)學
|
80.00
|
2
|
1
|
張三
|
數(shù)學
|
0.00
|
3
|
3
|
張三豐
|
體育
|
120.00
|
1
|
1
|
張三
|
語文
|
80.00
|
1
|
2
|
李四
|
語文
|
50.00
|
2
|
3
|
張三豐
|
語文
|
10.00
|
3
|
·查詢各科前2名(分區(qū)排名)
·類似:新聞表,求欄目點擊率在前3位的新聞。
商品表,求各類別銷售額在前10位的商品。
select * from ( select sc.s_id,sc.s_name,sub_name,sc.score, dense_rank() over (partition by sub_name order by score desc) 名次 from t_score sc ) x where x.名次=2
S_ID
|
S_NAME
|
SUB_NAME
|
SCORE
|
名次
|
4
|
楊過
|
JAVA
|
90.00
|
1
|
4
|
楊過
|
Oracle
|
77.00
|
1
|
2
|
李四
|
Oracle
|
77.00
|
1
|
3
|
張三豐
|
Oracle
|
0.00
|
2
|
5
|
mike
|
c++
|
80.00
|
1
|
3
|
張三豐
|
數(shù)學
|
|
1
|
2
|
李四
|
數(shù)學
|
80.00
|
2
|
3
|
張三豐
|
體育
|
120.00
|
1
|
1
|
張三
|
語文
|
80.00
|
1
|
2
|
李四
|
語文
|
50.00
|
2
|
S_ID
|
S_NAME
|
SUM_SCORE
|
1
|
張三
|
80.00
|
2
|
李四
|
207.00
|
3
|
張三豐
|
130.00
|
4
|
楊過
|
167.00
|
5
|
mike
|
80.00
|
S_ID
|
S_NAME
|
SUM_SCORE
|
名次
|
2
|
李四
|
207.00
|
1
|
4
|
楊過
|
167.00
|
2
|
3
|
張三豐
|
130.00
|
3
|
1
|
張三
|
80.00
|
4
|
5
|
mike
|
80.00
|
4
|
腳本:
create table t_score ( autoid number primary key, s_id number(3), s_name char(8) not null, sub_name varchar2(20), score number(10,2) ); insert into t_score (autoid, s_id, s_name, sub_name, score) values (8, 1, '張三 ', '語文', 80); insert into t_score (autoid, s_id, s_name, sub_name, score) values (9, 2, '李四 ', '數(shù)學', 80); insert into t_score (autoid, s_id, s_name, sub_name, score) values (10, 1, '張三 ', '數(shù)學', 0); insert into t_score (autoid, s_id, s_name, sub_name, score) values (11, 2, '李四 ', '語文', 50); insert into t_score (autoid, s_id, s_name, sub_name, score) values (12, 3, '張三豐 ', '語文', 10); insert into t_score (autoid, s_id, s_name, sub_name, score) values (13, 3, '張三豐 ', '數(shù)學', null); insert into t_score (autoid, s_id, s_name, sub_name, score) values (14, 3, '張三豐 ', '體育', 120); insert into t_score (autoid, s_id, s_name, sub_name, score) values (15, 4, '楊過 ', 'java', 90); insert into t_score (autoid, s_id, s_name, sub_name, score) values (16, 5, 'mike ', 'c++', 80); insert into t_score (autoid, s_id, s_name, sub_name, score) values (3, 3, '張三豐 ', 'oracle', 0); insert into t_score (autoid, s_id, s_name, sub_name, score) values (4, 4, '楊過 ', 'oracle', 77); insert into t_score (autoid, s_id, s_name, sub_name, score) values (17, 2, '李四 ', 'oracle', 77); commit;