Oracle分析函式八 CUBE,ROLLUP

2021-09-22 04:10:17 字數 3489 閱讀 7805

oracle 分析函式——cube , rollup

cube

功能描述:

注意:rollup

功能描述:

注意:如果是 rollup(a, b, c) 的話, group by 順序

(a 、 b 、 c)

(a 、 b)

(a)最後對全表進行 group by 操作。

如果是 group by cube(a, b, c) , group by 順序

(a 、 b 、 c)

(a 、 b)

(a 、 c)

(a) ,

(b 、 c)

(b)(c) ,

最後對全表進行 group by 操作。

create table studentscore (

student_name varchar2(20),

subjects varchar2(20),

score number )

insert into studentscore values('wbq','english',90);

insert into studentscore values('wbq','maths',95);

insert into studentscore values('wbq','chinese',88);

insert into studentscore values('czh','english',80);

insert into studentscore values('czh','maths',90);

insert into studentscore values('czh','history',92);

insert into studentscore values('cb','politics',70);

insert into studentscore values('cb','history',75);

insert into studentscore values('ldh','politics',80);

insert into studentscore values('ldh','chinese',90);

insert into studentscore values('ldh','history',95);

select

student_name,

subjects,

sum(score)

from studentscore

group by cube(student_name,subjects);

等同於以下標準 sql

select null,subjects,sum(score)

from studentscore

group by subjects

union

select student_name,null,sum(score)

from studentscore

group by student_name

union

select null,null,sum(score)

from studentscore

union

select student_name,subjects,sum(score)

from studentscore

group by student_name,subjects

select

student_name,

subjects,

sum(score)

from studentscore

group by rollup(student_name,subjects);

select student_name,null,sum(score)

from studentscore

group by student_name

union

select null,null,sum(score)

from studentscore

union

select student_name,subjects,sum(score)

from studentscore

group by student_name,subjects

select

grouping(student_name),

grouping(subjects), 

student_name,

subjects,

sum(score)

from studentscore

group by cube(student_name,subjects)

order by 1,2;

select

grouping(student_name),

grouping(subjects), 

student_name,

subjects,

sum(score)

from studentscore

group by rollup(student_name,subjects)

order by 1,2;

select

grouping_id(student_name,subjects),  

student_name,

subjects,

sum(score)

from studentscore

group by cube(student_name,subjects)

order by 1;

select

grouping_id(student_name,subjects),  

student_name,

subjects,

sum(score)

from studentscore

group by rollup(student_name,subjects)

order by 1;

select

grouping(student_name),

grouping(subjects),

case when grouping(student_name)=0 and grouping(subjects)=1 then ' 學生成績合計 '

when grouping(student_name)=1 and grouping(subjects)=0 then ' 課目成績合計 '

when grouping(student_name)=1 and grouping(subjects)=1 then ' 總                 計 '

else ''

end summary,

student_name,

subjects,

sum(score)

from studentscore

group by cube(student_name,subjects)

order by 1,2;

Oracle分析函式八 CUBE,ROLLUP

oracle 分析函式 cube rollup cube 功能描述 注意 rollup 功能描述 注意 如果是 rollup a,b,c 的話,group by 順序 a b c a b a 最後對全表進行 group by 操作。如果是 group by cube a,b,c group by 順...

Oracle分析函式八 CUBE,ROLLUP

oracle 分析函式 cube,rollup cube 功能描述 注意 rollup 功能描述 注意 如果是rollup a,b,c 的話,group by順序 a b c a b a 最後對全表進行 group by 操作。如果是group by cube a,b,c group by順序 a ...

Oracle分析函式八 CUBE,ROLLUP

oracle 分析函式 cube,rollup cube 功能描述 注意 rollup 功能描述 注意 如果是 rollup a,b,c 的話,group by 順序 a b c a b a 最後對全表進行 group by 操作。如果是 group by cube a,b,c group by 順...