047-020

20. In which scenario would you use the ROLLUP operator for expression or
columns within a GROUP BY clause?
A. to find the groups forming the subtotal in a row
B to create group-wrise grand totals for the groups specified within a GROUP
BY clause
C. to create a grouping for expressions or columns specified within a GROUP
BY clause in one direction,from right to left for calculating the subtotals
D. to create a grouping for expressions or columns specified within a GROUP
BY clause in all possible directions, which is cross-tabular report for
calculating the subtotals
Answer: C
Calculate:计划 subtotal:求..部分和 direction:用法 cross:交叉tabula:列表
The CUBE operation is something of a threedimensional version of ROLLUP. CUBE goes
beyond the functionality of ROLLUP by calculating subtotals for every possible
grouping within the columns selected and grouped. The CUBE is part of the GROUP BY
and as such is parsed as part of the GROUP BY clause within the overall execution of
the SELECT statement.
Rollup,cube子句在ORACLE中可以用来做交叉报表,如:如总计,小计类的功能很容易实
现;但是必须和group by 子句一起使用。举例如下:
Oracle的Cube,Rollup子句的用法产生交叉报表的情况,演示一个cube的例子
create table test(sales varchar2(10),dest varchar2(10),revenue number);
insert into test values('smith','hangzhou',1000);
insert into test values('smith','wenzhou',2000);
insert into test values('allen','wenzhou',3000);
insert into test values('allen','wenzhou',4000);
SALES DEST REVENUE
---------- ---------- ----------
smith hangzhou 1000
smith wenzhou 2000
allen wenzhou 3000
allen wenzhou 4000
比如说我们想统计每个sales的总销售收入,每个sales在各个城市的销售收入,另外还想知道
每个城市所有sales的销售收入总额,以及所有sales的总收入,就像以下报表
名字 hangzhou wenzhou 总计
------- ------------ ----------- ---------
allen 7000 7000
smith 1000 2000 3000
1000 9000 10000
那我们运行下面这条语句就行了
select sales,dest,sum(revenue) from test group by cube(sales,dest);
SALES DEST SUM(REVENUE)
---------- ---------- ------------
10000 所有sales的总销售收入
wenzhou 9000 所有sales在温州的销售收入
hangzhou 1000 所有sales在杭州的销售收入
allen 7000 allen的所有销售收入
allen wenzhou 7000 allen在温州的销售收入
smith 3000 smith所有的销售收入
smith wenzhou 2000 smith在温州的销售收入
smith hangzhou 1000 smith在杭州的销售收入
8 rows selected.
如果不想统计城市这个维度,那么用rollup子句
hangzhou wenzhou
allen 7000
7000
smith 1000 2000
3000
10000
select sales,dest,sum(revenue) from test group by rollup(sales,dest)
SALES DEST SUM(REVENUE)
---------- ---------- ------------------
allen wenzhou 7000 allen在温州的销售收入
allen 7000 allen的所有销售收入
smith wenzhou 2000 smith在温州的销售收入
smith hangzhou 1000 smith在杭州的销售收入
smith 3000 smith 所有的销售收入
10000 所有sales的总销售收入

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11312660/viewspace-718775/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11312660/viewspace-718775/

你可能感兴趣的:(047-020)