Group functions
SELECT [column,] group_function(column) ... FROM table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column];
e.g.:
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];
GROUP BY with ROLLUP and CUBE Operators
1. Use ROLLUP or CUBE with GROUP BY to produce superaggregate rows by cross-referencing columns.
2. ROLLUP grouping produces a result set containing the regular grouped rows and the subtotal values.
3. CUBE grouping produces a result set containing the rows from ROLLUP and cross-tabulation rows.
SELECT [column,] group_function(column). . .FROM table [WHERE condition] [GROUP BY [ROLLUP] group_by_expression] [HAVING having_expression];[ORDER BY column];
//ROLLUP is an extension to the GROUP BY clause Use the ROLLUP operation to produce cumulative
aggregates, such as subtotals.
e.g. :
select nmoduleinfoid, ndocsortid, salary from t1;
1 1 1000
1 2 2100
1 3 1300
2 4 1400
select nmoduleinfoid, ndocsortid, sum(salary) from t1 group by rollup(nmoduleinfoid, ndocsortid);
1 1 1000
1 2 2100
1 3 1300
1 4400
2 4 1400
2 1400
5800
SELECT [column,] group_function(column)... FROM table [WHERE condition] [GROUP BY [CUBE] group_by_expression] [HAVING having_expression] [ORDER BY column];
// CUBE is an extension to the GROUP BY clause. You can use the CUBE operator to produce cross-tabulation
values with a single SELECT statement.
select nmoduleinfoid, ndocsortid, sum(salary) from t1 group by cube(nmoduleinfoid, ndocsortid);
5800
1 1000
2 2100
3 1300
4 1400
1 4400
1 1 1000
1 2 2100
1 3 1300
2 1400
2 4 1400
SELECT [column,] group_function(column) .. , GROUPING(expr) FROM table [WHERE condition] [GROUP BY [ROLLUP][CUBE] group_by_expression] [HAVING having_expression] [ORDER BY column];
// The GROUPING function: • Is used with either the CUBE or ROLLUP operator • Is used to find the groups forming the subtotal in a row • Is used to differentiate stored NULL values from NULL values created by ROLLUP or CUBE • Returns 0 or 1
select nmoduleinfoid, ndocsortid, sum(salary), grouping(nmoduleinfoid) as m1,grouping(ndocsortid) as d1 from t1 group by rollup(nmoduleinfoid, ndocsortid);
1 1 1000 0 0
1 2 2100 0 0
1 3 1300 0 0
1 4400 0 1
2 4 1400 0 0
2 1400 0 1
5800 1 1
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.
e.g.:
SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY ROLLUP( department_id,(job_id, manager_id));
SELECT department_id, job_id, manager_id, SUM(salary) FROM employees GROUP BY department_id, ROLLUP(job_id), CUBE(manager_id);
-----------------------------------------------------------------------------------------------------------------------------------
当你与COUNT和SUM这类总计函数一起使用GROUP BY语句时,你一般得不到多级总数。GROUP BY中每个唯一的列组合生成一个总数,但这些总数不会“累加”到更高一级的总数中。
要实现这一点,你可以用GROUP BY ROLLUP或GROUP BY CUBE替代GROUP BY,不过它们会生成所有可能的总数,而你可能不需要全部总数。对GROUP BY CUBE而言,将会生成2^n组总数,这里的n是GROUP BY中列的数目。
查看下面的查询,它使用了SH样本模式:
SELECT prod_id, cust_id, channel_id, SUM(quantity_sold)
FROM sales
WHERE cust_id < 3
GROUP BY CUBE (prod_id, cust_id, channel_id)
这将生成8组总数:
可能的组合非常多。GROUP BY CUBE中每增加一列,生成的总数就会翻一番。
可以用GROUP BY GROUPING SETS来代替GROUP BY CUBE。你可以应用来指定你感兴趣的总数组合。因为它不必计算它不需要集合(也不会产生太多结果),所以对SQL引擎来说更为高效。
其格式为:
GROUP BY GROUPING SETS ((list), (list) ... )
这里(list)是圆括号中的一个列序列,这个组合生成一个总数。要增加一个总和,必须增加一个(NUlL)分组集。
例如,如果只要生成每项产品(包括所有顾客和通道)和每个顾客/通道组合(包括所有产品)的总数,可以输入:
SELECT prod_id, cust_id, channel_id, SUM(quantity_sold)
FROM sales
WHERE cust_id < 3
GROUP BY GROUPING SETS (
(prod_id), (cust_id, channel_id)
);
这种方法将这个数据集生成的总数数量从180个减少到37个,并帮助你着重回答你希望解答的问题。
---------------------------------------------------------------------------------------------------------
1、CUBE 和 ROLLUP区别:
CUBE 生成的结果集显示了所选列中值的所有组合的聚合。
ROLLUP 生成的结果集显示了所选列中值的某一层次结构的聚合。
2、GROUPING是一个聚合函数,它产生一个附加的列,当用 CUBE 或 ROLLUP 运算符添加行时,附加的列输出值为1,当所添加的行不是由 CUBE 或 ROLLUP 产生时,附加列值为0。
仅在与包含 CUBE 或 ROLLUP 运算符的 GROUP BY 子句相联系的选择列表中才允许分组。
----------------------------
Controlling the environment:
SET HEADING OFF
SET ECHO OFF
SET FEEDBACK OFF
SET PAGESIZE 0
-- sql statement....
SET HEADING ON
SET FEEDBACK ON
SET PAGESIZE 24
SET ECHO ON
........
COLUMN my_col NEW_VALUE dyn_where_clause
SELECT DECODE('&&deptno', null,
DECODE ('&&hiredate', null, ' ',
'WHERE hire_date=TO_DATE('''||'&&hiredate'',''DD-MON-YYYY'')'),
DECODE ('&&hiredate', null,
'WHERE department_id = ' || '&&deptno',
'WHERE department_id = ' || '&&deptno' ||
' AND hire_date = TO_DATE('''||'&&hiredate'',''DD-MON-YYYY'')'))
AS my_col FROM dual;
Hierarchical Queries
SELECT [LEVEL], column, expr... FROM table [WHERE condition(s)] [START WITH condition(s)] [CONNECT BY PRIOR condition(s)]
condition:
expr comparison_operator expr
1. Starting point: Specifies the condition that must be met, Accepts any valid condition
START WITH column1 = value
e.g.: start with cname='xxx'
2. CONNECT BY PRIOR column1 = column2
Walk from the top down, using the EMPLOYEES table.
e.g.: ... connect by prior employee_id = manager_id
direction:
Top down: Column1 = Parent Key , Column2 = Child Key
Bottom up: Column1 = Child Key, Column2 = Parent Key
e.g.
select entityid, entityname, entityparentid, entitydepth from tbentity
start with entityid=101
connect by prior entityid = entityparentid
e.g.:
select LPAD(entityname, LENGTH(entityname)+(LEVEL*2)-2,'_') AS org_chart
from tbentity
start with entityid=101
connect by prior entityid = entityparentid