ROLLUP,是GROUP BY子句的一种扩展,可以为每个分组返回小计记录以及为所有分组返回总计记录。
CUBE,也是GROUP BY子句的一种扩展,可以返回每一个列组合的小计记录,同时在末尾加上总计记录。
在文章的最后附上了相关表和记录创建的脚本。
1、向ROLLUP传递一列
SQL> select division_id,sum(salary)
2 from employees2
3 group by rollup(division_id)
4 order by division_id;
DIV SUM(SALARY)
--- -----------
BUS 1610000
OPE 1320000
SAL 4936000
SUP 1015000
8881000
SQL>
再来看一下如果使用普通的GROUP BY,而没有ROLLUP是个什么情况
SQL> select division_id,sum(salary)
2 from employees2
3 group by division_id
4 order by division_id;
DIV SUM(SALARY)
--- -----------
BUS 1610000
OPE 1320000
SAL 4936000
SUP 1015000
可以看到,缺少了最后的统计信息。
2、向ROLLUP传递多列
SQL> select division_id,job_id,sum(salary)
2 from employees2
3 group by rollup(division_id,job_id)
4 order by division_id,job_id;
DIV JOB SUM(SALARY)
--- --- -----------
BUS MGR 530000
BUS PRE 800000
BUS WOR 280000
BUS 1610000
OPE ENG 245000
OPE MGR 805000
OPE WOR 270000
OPE 1320000
SAL MGR 4446000
SAL WOR 490000
SAL 4936000
DIV JOB SUM(SALARY)
--- --- -----------
SUP MGR 465000
SUP TEC 115000
SUP WOR 435000
SUP 1015000
8881000
16 rows selected.
可以看到,除了在最后有一个求和记录外,每个division_id分组也会有一个求和记录。
那么我们现在交换一下ROLLUP中数据列的顺序,看看结果怎样
SQL> select job_id,division_id,sum(salary)
2 from employees2
3 group by rollup(job_id,division_id)
4 order by job_id,division_id;
JOB DIV SUM(SALARY)
--- --- -----------
ENG OPE 245000
ENG 245000
MGR BUS 530000
MGR OPE 805000
MGR SAL 4446000
MGR SUP 465000
MGR 6246000
PRE BUS 800000
PRE 800000
TEC SUP 115000
TEC 115000
JOB DIV SUM(SALARY)
--- --- -----------
WOR BUS 280000
WOR OPE 270000
WOR SAL 490000
WOR SUP 435000
WOR 1475000
8881000
17 rows selected.
结果类似,只不过是每一个job_id分组有一个求和记录罢了。
3、向CUBE传递一列
SQL> select division_id,sum(salary)
2 from employees2
3 group by cube(division_id)
4 order by division_id;
DIV SUM(SALARY)
--- -----------
BUS 1610000
OPE 1320000
SAL 4936000
SUP 1015000
8881000
好像和ROLLUP没什么区别哦,呵呵,继续往下看。
4、向CUBE传递多列
SQL> select job_id,division_id,sum(salary)
2 from employees2
3 group by cube(job_id,division_id)
4 order by job_id,division_id;
JOB DIV SUM(SALARY)
--- --- -----------
ENG OPE 245000
ENG 245000
MGR BUS 530000
MGR OPE 805000
MGR SAL 4446000
MGR SUP 465000
MGR 6246000
PRE BUS 800000
PRE 800000
TEC SUP 115000
TEC 115000
JOB DIV SUM(SALARY)
--- --- -----------
WOR BUS 280000
WOR OPE 270000
WOR SAL 490000
WOR SUP 435000
WOR 1475000
BUS 1610000
OPE 1320000
SAL 4936000
SUP 1015000
8881000
21 rows selected.
可以看到工资是根据job_id和division_id求和的,CUBE在每一个job_id中都返回一条记录,表示其中的工资总数,同时在接近末尾处显示每一种division_id的工资总数,最后一条记录显示所有工资的总数。
把两列的顺序换换会怎样?呵呵,真的有兴趣那就自己动手试试吧。
==================================================================================
CREATE TABLE divisions (
division_id CHAR(3) CONSTRAINT divisions_pk PRIMARY KEY,
name VARCHAR2(15) NOT NULL
);
CREATE TABLE jobs (
job_id CHAR(3) CONSTRAINT jobs_pk PRIMARY KEY,
name VARCHAR2(20) NOT NULL
);
CREATE TABLE employees2 (
employee_id INTEGER CONSTRAINT employees2_pk PRIMARY KEY,
division_id CHAR(3)
CONSTRAINT employees2_fk_divisions
REFERENCES divisions(division_id),
job_id CHAR(3) REFERENCES jobs(job_id),
first_name VARCHAR2(10) NOT NULL,
last_name VARCHAR2(10) NOT NULL,
salary NUMBER(6, 0)
);
INSERT INTO divisions (
division_id, name
) VALUES (
'SAL', 'Sales'
);
INSERT INTO divisions (
division_id, name
) VALUES (
'OPE', 'Operations'
);
INSERT INTO divisions (
division_id, name
) VALUES (
'SUP', 'Support'
);
INSERT INTO divisions (
division_id, name
) VALUES (
'BUS', 'Business'
);
INSERT INTO jobs (
job_id, name
) VALUES (
'WOR', 'Worker'
);
INSERT INTO jobs (
job_id, name
) VALUES (
'MGR', 'Manager'
);
INSERT INTO jobs (
job_id, name
) VALUES (
'ENG', 'Engineer'
);
INSERT INTO jobs (
job_id, name
) VALUES (
'TEC', 'Technologist'
);
INSERT INTO jobs (
job_id, name
) VALUES (
'PRE', 'President'
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
1, 'BUS', 'PRE', 'James', 'Smith', 800000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
2, 'SAL', 'MGR', 'Ron', 'Johnson', 350000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
3, 'SAL', 'WOR', 'Fred', 'Hobbs', 140000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
4, 'SUP', 'MGR', 'Susan', 'Jones', 200000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
5, 'SAL', 'WOR', 'Rob', 'Green', 350000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
6, 'SUP', 'WOR', 'Jane', 'Brown', 200000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
7, 'SUP', 'MGR', 'John', 'Grey', 265000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
8, 'SUP', 'WOR', 'Jean', 'Blue', 110000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
9, 'SUP', 'WOR', 'Henry', 'Heyson', 125000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
10, 'OPE', 'MGR', 'Kevin', 'Black', 225000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
11, 'OPE', 'MGR', 'Keith', 'Long', 165000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
12, 'OPE', 'WOR', 'Frank', 'Howard', 125000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
13, 'OPE', 'WOR', 'Doreen', 'Penn', 145000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
14, 'BUS', 'MGR', 'Mark', 'Smith', 155000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
15, 'BUS', 'MGR', 'Jill', 'Jones', 175000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
16, 'OPE', 'ENG', 'Megan', 'Craig', 245000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
17, 'SUP', 'TEC', 'Matthew', 'Brant', 115000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
18, 'OPE', 'MGR', 'Tony', 'Clerke', 200000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
19, 'BUS', 'MGR', 'Tanya', 'Conway', 200000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
20, 'OPE', 'MGR', 'Terry', 'Cliff', 215000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
21, 'SAL', 'MGR', 'Steve', 'Green', 275000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
22, 'SAL', 'MGR', 'Roy', 'Red', 375000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
23, 'SAL', 'MGR', 'Sandra', 'Smith', 335000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
24, 'SAL', 'MGR', 'Gail', 'Silver', 225000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
25, 'SAL', 'MGR', 'Gerald', 'Gold', 245000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
26, 'SAL', 'MGR', 'Eileen', 'Lane', 235000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
27, 'SAL', 'MGR', 'Doreen', 'Upton', 235000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
28, 'SAL', 'MGR', 'Jack', 'Ewing', 235000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
29, 'SAL', 'MGR', 'Paul', 'Owens', 245000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
30, 'SAL', 'MGR', 'Melanie', 'York', 255000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
31, 'SAL', 'MGR', 'Tracy', 'Yellow', 225000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
32, 'SAL', 'MGR', 'Sarah', 'White', 235000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
33, 'SAL', 'MGR', 'Terry', 'Iron', 225000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
34, 'SAL', 'MGR', 'Christine', 'Brown', 247000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
35, 'SAL', 'MGR', 'John', 'Brown', 249000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
36, 'SAL', 'MGR', 'Kelvin', 'Trenton', 255000
);
INSERT INTO employees2 (
employee_id, division_id, job_id, first_name, last_name, salary
) VALUES (
37, 'BUS', 'WOR', 'Damon', 'Jones', 280000
);
相关推荐
Oracle数据库中的ROLLUP和CUBE是两种用于生成数据汇总的SQL操作,它们在数据分析和报告制作中非常有用。本文将详细介绍这两个概念以及如何在实际应用中使用它们。 首先,我们来理解`ROLLUP`。ROLLUP操作是GROUP BY...
当我们需要更复杂的分组和汇总信息时,可以使用扩展的聚合功能,如ROLLUP、CUBE以及GROUPING()和GROUPING_ID()函数。这些功能允许我们生成多层次的总计和小计,为数据分析提供了极大的灵活性。 1. ROLLUP子句: ...
### Oracle中的ROLLUP与CUBE使用详解 在Oracle数据库中,`ROLLUP`与`CUBE`是非常重要的分组函数,被广泛应用于数据汇总、数据分析等场景中。这两个功能可以帮助开发人员快速地对数据进行多维度分析,是开发人员必备...
NULL 博文链接:https://wangjingyi.iteye.com/blog/1545090
而在GROUP BY的基础上,Oracle数据库提供了两种高级分组功能:ROLLUP和CUBE,它们允许我们更灵活地生成汇总数据。 1. ROLLUP(滚联回溯): ROLLUP是GROUP BY的一个扩展,它不仅返回每个单独的分组结果,还会生成...
总结来说,Oracle的`ROLLUP`和`CUBE`函数是强大的数据分析工具,能够帮助用户快速生成多层次的汇总数据,适用于各种复杂的数据报告需求。在处理大量数据时,它们的简洁语法和高效性能使得数据聚合变得更为方便。理解...
在 Oracle 中,除了 Grouping Sets 之外,还有其他两种分组统计方法:Rollup 和 Cube。Rollup 是一种统计方法,它可以对数据进行分组和聚合计算,但它只能对单个列进行分组。Cube 是一种特殊的分组统计方法,它可以...
例如,可以使用 Rollup 函数对数据进行分组和汇总,使用 Rank 函数对数据进行排名,使用 Lag 和 Lead 函数对数据进行延迟和前导操作,使用 Sum 和 Avg 函数对数据进行汇总和平均计算,使用 Ratio_to_Report 函数对...
PL/SQL函数和过程可以使用Oracle的函数和过程语法来编写,而Transact-SQL函数和过程则需要使用SqlServer的函数和过程语法。 例如,Oracle的PL/SQL函数可以这样编写: ```plsql CREATE OR REPLACE FUNCTION get_...
[WITH {CUBE|ROLLUP}] HAVING search_condition ORDER BY order_expression ``` - **转换策略:** - 移除Oracle特有的基于成本的优化提示。 - 使用SQL Server的优化器。 - 使用递归CTEs替代`START WITH ... ...
因此,为了优化性能和减少不必要的结果,我们可能需要配合使用ROLLUP或GROUPING SETS,它们提供了更细粒度的控制来生成特定的分组组合。同时,我们还可以使用HAVING子句来过滤不感兴趣的结果,比如去除包含NULL的...
此外,还会学习一些高级技术,比如使用ROLLUP和CUBE进行数据聚合操作、使用Set运算符进行集合操作以及分级获取数据等高级特性。掌握这些基础知识和技能对于未来进行数据库的深入学习和使用是非常重要的。 针对...
GROUP BY with ROLLUP and CUBE Operators 3-6 ROLLUP Operator 3-7 ROLLUP Operator Example 3-8 CUBE Operator 3-9 CUBE Operator: Example 3-10 GROUPING Function 3-11 GROUPING Function: Example 3-12 ...
1. **自动汇总函数 (ROLLUP, CUBE)** - `ROLLUP` 用于创建多级汇总,它会生成一个从最细粒度级别到最粗粒度级别的层次结构,类似于Excel中的数据透视表的行标签部分。 - `CUBE` 则生成所有可能的组合,包括单列、...
3. **Oracle特有功能**:Oracle数据库提供了一些特有的SQL语法和功能,如游标(CURSOR)、PL/SQL(Oracle的存储过程语言)、分组函数(GROUP BY, ROLLUP, CUBE)以及窗口函数(OVER()子句)。 4. **数据类型**:...
9. 分组和分析函数:如GROUP BY、ROLLUP、CUBE和RANK等,用于数据的分组汇总和复杂分析,是大数据处理和OLAP操作的关键。 10. 连接函数:如CONNECT_BY_ROOT和START_WITH,用于构建复杂的树状查询,处理层次结构数据...
6. 探索高级SQL查询技术,如合并查询(MERGE)、并集(UNION ALL)、交集(INTERSECT)、差集(MINUS),以及GROUP BY、ROLLUP、CUBE等聚合函数,提升数据提取效率。 7. 学习编写SQL脚本,生成报表样式的输出结果,...
基础数据示例展示了bill_month(账单月份)、area_code(区域代码)、net_type(网络类型)和local_fare(本地费用)四个字段,我们可以使用上述分析函数对这些数据进行多维度的分析,例如按区域和网络类型分组,...