oracle分組排序

2021-09-30 15:42:13 字數 3538 閱讀 1878

在做統計是很多情況會用到分組排序 結合談論的方法

自己建了乙個demo驗證 留在這裡以便備份

create table student2(

student_id number primary key,

student_name varchar2(30) not null)

create table score(

score_id number primary key,

student_id number,

course_id number,

score number)

create table course(

course_id number primary key,

course_name varchar2(30)) ;

insert into course values (1,'課程1');

insert into course values (2,'課程2');

insert into course values (3,'課程3');

insert into course values (4,'課程4');

insert into score values (1,1,1,90);

insert into score values (2,1,2,89);

insert into score values (3,1,3,88);

insert into score values (4,1,4,85);

insert into score values (5,2,1,86);

insert into score values (6,2,2,80);

insert into score values (7,2,3,56);

insert into score values (8,2,4,98);

insert into score values (9,3,1,67);

insert into score values (10,3,4,34);

insert into score values (11,3,2,67);

insert into score values (12,3,3,90);

insert into score values (13,4,1,91);

insert into score values (14,4,2,92);

insert into score values (15,4,3,93);

insert into score values (16,4,4,94);

insert into score values (17,5,1,98);

insert into score values (18,5,2,99);

insert into score values (45,5,3,98);

insert into score values (46,5,4,99);

insert into score values (19,6,1,88);

insert into score values (20,6,2,89);

insert into score values (21,6,3,88);

insert into score values (22,6,4,89);

insert into score values (23,7,1,78);

insert into score values (24,7,2,79);

insert into score values (25,7,3,78);

insert into score values (26,7,4,79);

insert into score values (27,8,1,71);

insert into score values (28,8,2,72);

insert into score values (29,8,3,73);

insert into score values (30,8,4,74);

insert into score values (31,9,1,61);

insert into score values (32,9,2,62);

insert into score values (33,9,3,63);

insert into score values (34,9,4,64);

insert into score values (35,10,1,55);

insert into score values (36,10,2,65);

insert into score values (37,10,3,53);

insert into score values (38,10,4,54);

insert into student2 values (1,'張1');

insert into student2 values (2,'張2');

insert into student2 values (3,'張3');

insert into student2 values (4,'張4');

insert into student2 values (5,'張5');

insert into student2 values (6,'張6');

insert into student2 values (7,'張7');

insert into student2 values (8,'張8');

insert into student2 values (9,'張9');

insert into student2 values (10,'張10');

用rank() dense_rank()具有並列名次之分

select *

from (select a.score,

b.student_name,

c.course_name,

rank() over(partition by a.course_id order by a.score desc) rk

from score a, student2 b, course c

where a.student_id = b.student_id

and a.course_id = c.course_id) ss

where ss.rk <= 4

而採用row_number()是直接去行數  不管是否具有相同資料

select *

from (select a.score,

b.student_name,

c.course_name,

row_number() over(partition by a.course_id order by a.score desc) rk

from score a, student2 b, course c

where a.student_id = b.student_id

and a.course_id = c.course_id) ss

where ss.rk <= 4

oracle 實現分組排序

select t.t.rowid,rank over partition by t.classid order by t.score,t.classid from tablename t 按照classid分組,並按照classid公升序,同時分數公升序排列。注意 對於每個分組,比如classid ...

Oracle 分組排序函式

專案開發中,我們有時會碰到需要分組排序來解決問題的情況 1 要求取出按field1分組後,並在每組中按照field2排序 2 亦或更加要求取出1中已經分組排序好的前多少行的資料 這裡通過一張表的示例和sql語句闡述下oracle資料庫中用於分組排序函式的用法。1.row number over ro...

Oracle 分組排序問題

a b兩個表,分別存的是使用者的收入和支出 a表 id date user income fid fdate fuser fincome 1 2018 8 1 張三 11.00 2 2018 8 2 張三 12.00 3 2018 8 2 李四 21.00 4 2018 8 3 張三 13.00 5...