Oracle_071_lesson_p21

Generating Reports by Grouping Related Data使用分组生成报告

you should be able to use the:
1、ROLLUP operation to produce subtotal values
2、CUBE operation to produce cross-tabulation values
3、GROUPING function to identify the row values created by ROLLUP or CUBE
4、GROUPING SETS to produce a single result set

group
SELECT [column,] group_function(column). . .
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];

例:
SELECT AVG(salary), STDDEV(salary),
COUNT(commission_pct),MAX(hire_date)
FROM employees
WHERE job_id LIKE 'SA%';

SELECT [column,]
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];

SELECT department_id, job_id, SUM(salary),
COUNT(employee_id)
FROM employees
GROUP BY department_id, job_id ;

SELECT [column,] group_function(column)...
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING having_expression]
[ORDER BY column];

ROLLUP操作:从右往左减列分别运算输出小计
SELECT [column, ]group_function(column). . .
FROM table
[WHERE condition]
[GROUP BY ROLLUP group_by_expression]
[HAVING having_expression];
[ORDER BY column];

例:
SELECT department_id, job_id, SUM(salary)
FROM employees
WHERE department_id < 60
GROUP BY ROLLUP(department_id, job_id);

COBE操作:除了ROLLUP的操作,还要从左往右减列分别运算输出小计
SELECT [column, ] group_function(column)...
FROM table
[WHERE condition]
[GROUP BY CUBE group_by_expression]
[HAVING having_expression]
[ORDER BY column];

例:
SELECT department_id, job_id, SUM(salary)
FROM employees
WHERE department_id < 60
GROUP BY CUBE (department_id, job_id) ;
!

GROUPING 函数
The GROUPING function:
1、Is used with either the CUBE or ROLLUP operator
2、Is used to find the groups forming the subtotal in a row
3、Is used to differentiate stored NULL values from NULL values created by ROLLUP or CUBE
4、Returns 0 or 1

SELECT [column,] group_function(column) .. ,
GROUPING(expr)
FROM table
[WHERE condition]
[GROUP BY [ROLLUP][CUBE] group_by_expression]
[HAVING having_expression]
[ORDER BY column];

例:
SELECT department_id DEPTID, job_id JOB,
SUM(salary),
GROUPING(department_id) GRP_DEPT,
GROUPING(job_id) GRP_JOB
FROM employees
WHERE department_id < 50
GROUP BY ROLLUP(department_id, job_id);

GROUPING SETS
The GROUPING SETS syntax is used to define multiple groupings in the same query.
All groupings specified in the GROUPING SETS clause are computed and the results of individual groupings are combined with a UNION ALL operation.
Grouping set efficiency:
Only one pass over the base table is required.
There is no need to write complex UNION statements.
The more elements GROUPING SETS has, the greater is the performance benefit.

例:
SELECT department_id, job_id,
manager_id,AVG(salary)
FROM employees
GROUP BY GROUPING SETS ((department_id,job_id), (job_id,manager_id));

Composite Columns复合列
A composite column is a collection of columns that are treated as a unit.
ROLLUP (a, (b,c) , d)
Use parentheses within the GROUP BY clause to group columns, so that they are treated as a unit while computing ROLLUP or CUBE operations.
When used with ROLLUP or CUBE, composite columns would require skipping aggregation across certain levels.

例:
SELECT department_id, job_id, manager_id,
SUM(salary)
FROM employees GROUP BY ROLLUP( department_id,(job_id, manager_id));

Concatenated Groupings连接分组

Concatenated groupings offer a concise way to generate useful combinations of groupings.
To specify concatenated grouping sets, you separate multiple grouping sets, ROLLUP, and CUBE operations with commas so that the Oracle server combines them into a single GROUP BY clause.
The result is a cross-product of groupings from each GROUPING SET.
GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)

例:
SELECT department_id, job_id, manager_id,
SUM(salary)
FROM employees GROUP BY department_id,
ROLLUP(job_id),
CUBE(manager_id);

转载于:https://blog.51cto.com/3938853/2159858

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值