Oracle分组函数rollup,cube
rollup(字段1,字段2): cube(字段1,字段2): rollup(col1, col2,...) 和 cube(col1, col2,...) 用法区别在 cube 在
rollup(字段1,字段2):
cube(字段1,字段2):
rollup(col1, col2,...) 和 cube(col1, col2,...) 用法区别在 cube 在 rollup 汇总的记录集上,还会增加对 col2 等字段的汇总;
ROLLUP只对第一个参数(字段)进行汇总,CUBE可以对参数(字段)依次汇总,所以ROLLUP中参数个数只有一个会起作用(且排名在前的参数)。
Oracle的GROUP BY语句除了最基本的语法外,还支持ROLLUP和CUBE语句。
ROLLUP(A, B, C):
首先会对(A、B、C)进行GROUP BY,然后对(A、B)进行GROUP BY,然后是(A)进行GROUP BY,最后对全表进行GROUP BY操作。
CUBE(A, B, C),则首先会对(A、B、C)进行GROUP BY,然后依次是(A、B),(A、C),(A),(B、C),(B),(C),最后对全表进行GROUP BY操作。
CUBE 和 ROLLUP 之间的区别在于:
CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
例子:
create table student(
cgrade varchar2(64),
cclass varchar2(64),
cgroup varchar2(64),
stu int
)
insert into student(cgrade,cclass,cgroup,stu) values('1','1','1',10);
insert into student(cgrade,cclass,cgroup,stu) values('1','2','1',10);
insert into student(cgrade,cclass,cgroup,stu) values('1','2','2',20);
insert into student(cgrade,cclass,cgroup,stu) values('2','1','1',30);
insert into student(cgrade,cclass,cgroup,stu) values('2','2','2',40);
select * from student;
select cgrade,cclass,sum(stu) from student group by cgrade,cclass;
select cgrade,cclass,sum(stu) from student group by cube(cgrade,cclass);
select decode(grouping(cgrade),1,'学校人数',0,cgrade),decode(grouping(cclass)+grouping(cgrade),1,'年级人数',0,cclass),sum(stu) from student group by rollup(cgrade,cclass);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)
