ORACLE 分组和时间函数

Sql代码   收藏代码
  1. 使用下面的日期时间函数:   
  2. -TZ_OFFSET  
  3. -CURRENT_DATE  
  4. -CURRENT_TIMESTAMP  
  5. -LOCALTIMESTAMP  
  6. -DBTIMEZONE  
  7. -SESSIONTIMEZONE  
  8. -EXTRACT  
  9. -FROM_TZ  
  10. -TO_TIMESTAMP  
  11. -TO_TIMESTAMP_TZ  
  12. -TO_YMINTERVAL  
  13.   
  14.             oracle9i日期时间支持  
  15.   
  16. :: 在 Oracle9i 中,你能够在你的日期和时间数据中包含时区,并且提供对小数秒的支持  
  17. :: Oracle9i 中新的日期时间数据类型:      
  18.     -TIMESTAMP  
  19.     -TIMESTAMP WITH TIME ZONE (TSTZ)   
  20.     -TIMESTAMP WITH LOCAL TIME ZONE (TSLTZ)   
  21. :: Oracle9i 服务器提供对日期时间数据类型的夏令时支持  
  22.   
  23.   
  24.             --CURRENT_DATE  
  25.   
  26. :: 显示在会话时区中的当前日期和时间  
  27. ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';  
  28.   
  29. :: CURRENT_DATE 对于会话时区是敏感的  
  30.   
  31. Note:the ALTER SESSION command sets the date format of the session to 'DD-MON-YYYY HH24:MI:SS' that is Day of month(1-31)-Abbreviated name of month -4-digit year Hours of dat(0-23):Minute(0-59):Second(0-59)  
  32.   
  33.   
  34.             --CURRENT_TIMESTAMP  
  35.   
  36. :: 显示在会话时区中的当前日期和时间,包含小数秒  
  37. ALTER SESSION SET TIME_ZONE='-5:0';   //'-8.0'  
  38.   
  39. SELECT SESSIONTIMEZONE,CURRENT_TIMESTAMP FROM DUAL;  
  40.   
  41. ::CURRENT_TIMESTAMP 对于会话时区是敏感的  
  42. ::返回值是TIMESTAMP WITH TIME ZONE 数据类型  
  43.   
  44.   
  45.             --LOCALTIMESTAMP  
  46.   
  47. :: 以 TIMESTAMP 数据类型的值显示在会话时区中的当前日期和时间  
  48.   
  49. ALTER SESSION SET TIME_ZONE = '-5:0';  
  50.   
  51. SELECT CURRENT_TIMESTAMP, LOCALTIMESTAMP   
  52. FROM DUAL;  
  53.   
  54. :: LOCALTIMESTAMP 返回一个 TIMESTAMP 值,而 CURRENT_TIMESTAMP 返回一个 TIMESTAMP WITH TIME ZONE 值  
  55.   
  56.             --DBTIMEZONE 和 SESSIONTIMEZONE  
  57.   
  58. :: 显示数据库时区的值     
  59. SELECT DBTIMEZONE FROM DUAL;  
  60.   
  61. :: 显示会话时区的值  
  62.    SELECT SESSIONTIMEZONE FROM DUAL;  
  63.   
  64.   
  65.             --EXTRACT  
  66. :: 从SYSDATE 中显示年  
  67.    SELECT EXTRACT (YEAR FROM SYSDATE) FROM DUAL;  
  68.   
  69. :: 从 HIRE_DATE 中显示 MANAGER_ID 是100的雇员的月  
  70.   
  71. SELECT last_name, hire_date,   
  72.        EXTRACT (MONTH FROM HIRE_DATE)  
  73. FROM employees  
  74. WHERE manager_id = 100;  
  75.   
  76. LAST_NAME                 HIRE_DATE  EXTRACT(MONTHFROMHIRE_DATE)  
  77. ------------------------- ---------- ---------------------------  
  78. Kochhar                   21-9月 -89                           9  
  79. De Haan                   13-1月 -93                           1  
  80. Raphaely                  07-12月-94                          12  
  81. Weiss                     18-7月 -96                           7  
  82. Fripp                     10-4月 -97                           4  
  83. Kaufling                  01-5月 -95                           5  
  84.   
  85.   
  86. 语法:    
  87. SELECT  EXTRACT ([YEAR] [MONTH][DAY] [HOUR] [MINUTE][SECOND]                        [TIMEZONE_HOUR] [TIMEZONE_MINUTE]  
  88.         [TIMEZONE_REGION] [TIMEZONE_ABBR]  
  89. FROM    [datetime_value_expression]   
  90.     [interval_value_expression]);  
  91.   
  92. TIMESTAMP WITH LOCAL TIME ZONE 存储在数据库时区中,当一个用户选择数据时,值被调整到用户会话的时区。  
  93.   
  94.         --***************增强 GROUP BY 子句*********************  
  95.   
  96.             目标  
  97.   
  98. 完成本课后, 您应当能够:  
  99. :: 用 ROLLUP 操作产生小计值  
  100. :: 用 CUBE 操作产生交叉表值  
  101. :: 用 GROUPING 函数确定由 ROLLUP 或 CUBE 创建的行值  
  102. :: 用 GROUPING SETS 产生一个单个的结果集  
  103.   
  104.   
  105. Lesson Aim  
  106.   
  107.    In this lesson you learn how to:  
  108. :: Group data for obtaining the following:  
  109.     -Subtotal values by using the ROLLUP operator   
  110.     -Cross-tabulation values by using the CUBE operator  
  111. :: Use the GROUPING function to identify the level of aggregation in the results set produced by a ROLLUP or CUBE operator.  
  112. :: Use GROUPING SETS to produce a single result set(结果集) that is equivalent to a UNION ALL approach(方法,步骤).  
  113.   
  114.         --**************组函数的回顾*********************  
  115.   
  116. 组函数在行集上操作,给出分组结果  
  117. SELECT  [column,] group_function(column). . .  
  118. FROM    table  
  119. [WHERE  condition]  
  120. [GROUP BY   group_by_expression]  
  121. [ORDER BY   column];  
  122.   
  123. 例子:  
  124. SELECT AVG(salary), STDDEV(salary),  
  125.        COUNT(commission_pct),MAX(hire_date)  
  126. FROM   employees  
  127. WHERE  job_id LIKE 'SA%';  
  128.   
  129.   
  130.  Group Functions  
  131.    You can use the GROUP BY clause to divide the rows in a table into groups. You can then use the group functions to return summary information for each groupGroup functions can appear in select lists and in ORDER BY and HAVING clauses. The Oracle Server applies the group functions to each group of rows and returns a single result row for each group.   
  132.   
  133.  Types of Group Functions  
  134.     Each of the group functions AVGSUMMAXMINCOUNT, STDDEV, and VARIANCE accept one argument. The functions AVGSUM, STDDEV, and VARIANCE operate only on numeric values(只运算数字类型的值). MAX and MIN can operate on numericcharacteror date data valuesCOUNT returns the number of nonnull rows for the given expression.   
  135.   
  136.    The example in the slide calculates the average salary, standard deviation on the salary, number of employees earning a commission and the maximum hire date for those employees whose JOB_ID begins with SA.  
  137.   
  138.  Guidelines for Using Group Functions   ././././.  
  139. :: The data types for the arguments can be CHAR, VARCHAR2, NUMBER, or DATE.  
  140. :: All group functions except COUNT(*) ignore null valuesTo substitute(为NULL替换) a value for null values, use the NVL functionCOUNT returns either a number or zero.  
  141. :: The Oracle Server implicitly sorts(隐含) the results set in ascending order of the grouping columns specified(ASC), when you use a GROUP BY clause. To override this default ordering, you can use DESC in an ORDER BY clause.  
  142.   
  143.  Instructor Note  
  144.    You can skip this slide if the students are already familiar with these concepts.  
  145.   
  146.                 Group By 子句的回顾  
  147.   
  148. 语法:  
  149. SELECT  [column,] group_function(column). . .  
  150. FROM    table  
  151. [WHERE  condition]  
  152. [GROUP BY   group_by_expression]  
  153. [ORDER BY   column];  
  154.   
  155. Example:  
  156. SELECT department_id,job_id,SUM(salary),COUNT(employee_id)  
  157. FROM employees  
  158. GROUP BY department_id,job_id;  
  159.   
  160.   
  161. Review of GROUP BY Clause  (回顾)  
  162.   The example illustrated in the slide is evaluated by the Oracle Server as follows:  
  163.   
  164.      :: The SELECT clause specifies that the following columns are to be retrieved(找到):  
  165.     -Department ID and job ID columns from the EMPLOYEES table  
  166.     -The sum of all the salaries and the number of employees in each group that you          have specified in the GROUP BY clause   ../././  
  167.   
  168.      :: The GROUP BY clause specifies how the rows should be grouped in the table. The total salary and the number of employees are calculated for each job ID within each department. The rows are grouped by department ID and then grouped by job within each department.  
  169.   
  170.             HAVING 子句的回顾  
  171.   
  172. SELECT  [column,] group_function(column)...   
  173. FROM    table  
  174. [WHERE  condition]  
  175. [GROUP BY   group_by_expression]  
  176. [HAVING     having_expression]    
  177. [ORDER BY   column];  
  178.   
  179. :: 用HAVING 子句指定将被显示的那些组  
  180. :: 在限制条件的基础上可以进一步约束分组.  
  181.   
  182.   
  183. The HAVING Clause  
  184.   Groups are formed and group functions are calculated before the HAVING clause is applied to the groups. The HAVING clause can precede the GROUP BY clause, but it is recommended that you place the GROUP BY clause first because it is more logical.  
  185.   
  186. ./././././  
  187. The Oracle Server performs the following steps when you use the HAVING clause:  
  188.   
  189.   1.Groups rows   
  190.   2.Applies the group functions to the groups and displays the groups that match the criteria in the HAVING clause  
  191.     SELECT department_id, AVG(salary)  
  192.     FROM   employees  
  193.     GROUP  BY  department_id  
  194.     HAVING AVG(salary) >9500;  
  195.   
  196.     DEPARTMENT_ID       AVG(SALARY)  
  197.         80      10033.3434  
  198.         90      10036.4  
  199.         100     10032.34  
  200. the example dispalys depatment ID and average salary for those departments whose average salary is greater than $9,500  
  201.   
  202.   
  203.             GROUP BY 带 ROLLUP 或 CUBE 操作  
  204.   
  205. :: 带 ROLLUP 或 CUBE 的 GROUP BY 子句用交叉引用列产生超合计行  
  206. :: ROLLUP 分组产生一个包含常规分组行和subtotal的结果集  
  207. :: CUBE 分组产生一个包含 ROLLUP 行和交叉表行的结果集  
  208.   
  209. GROUP BY with the ROLLUP and CUBE Operators  
  210.    You specify ROLLUP and CUBE operators in the GROUP BY clause of a query. ROLLUP grouping produces(产生) a results set containing the regular grouped rows and subtotal rows. The CUBE operation in the GROUP BY clause groups the selected rows based on the values of all possible combinations of expressions in the specification and returns a single row of summary information for each group. You can use the CUBE operator to produce cross-tabulation rows.   
  211.   
  212. Note: When working with ROLLUP and CUBE, make sure that the columns following the GROUP BY clause have meaningful, real-life relationships with each other; otherwise the operators return irrelevant information(不相关的信息).   
  213.    
  214.   The ROLLUP and CUBE operators are available only in Oracle8i and later releases.   
  215.   
  216.             ROLLUP操作  
  217.   
  218. SELECT      [column,] group_function(column). . .  
  219. FROM        table  
  220. [WHERE      condition]  
  221. [GROUP BY   [ROLLUP] group_by_expression]  
  222. [HAVING     having_expression];  
  223. [ORDER BY   column];  
  224.   
  225. :: ROLLUP 是一个 GROUP BY 子句的扩展  
  226. :: 用 ROLLUP 操作产生小计和累计  
  227.   
  228. The ROLLUP Operator   
  229.      The ROLLUP operator delivers aggregates and superaggregates for expressions within a GROUP BY statement. The ROLLUP operator can be used by report writers to extract statistics and summary information from results sets.(从结果集中得到精确的计算和信息概要) The cumulative aggregates can be used in reports, charts, and graphs.   
  230.   
  231.   
  232.     The ROLLUP operator creates groupings by moving in one direction(从一个方向移动), from right to left, along the list of columns specified in the GROUP BY clause. It then applies the aggregate function to these groupings.   
  233.   
  234. /././././.  
  235.     Note: To produce subtotals in n dimensions (that is, n columns in the GROUP BY clause) without a ROLLUP operator, n+1 SELECT statements must be linked with UNION ALL. (如果没有rollup操作,而产生N元小计值,要使用N+1个select操作用NUION ALL连接)This makes the query execution inefficient, because each of the SELECT statements causes table access. The ROLLUP operator gathers its results with just one table access. The ROLLUP operator is useful if there are many columns involved(许多与列有关的) in producing the subtotals.  
  236.   
  237.             --ROLLUP 操作的例子  
  238.   
  239. SELECT   department_id, job_id, SUM(salary)  
  240. FROM     employees    
  241. WHERE    department_id < 60  
  242. GROUP BY ROLLUP(department_id, job_id);  
  243.   
  244. DEPARTMENT_ID JOB_ID     SUM(SALARY)  
  245. ------------- ---------- -----------  
  246.            10 AD_ASST           4400  
  247.            10                   4400  
  248.            20 MK_MAN           13000  
  249.            20 MK_REP            6000  
  250.            20                  19000  
  251.            30 PU_MAN           11000  
  252.            30 PU_CLERK         13900  
  253.            30                  24900  
  254.            40 HR_REP            6500  
  255.            40                   6500  
  256.            50 ST_MAN           36400  
  257.            50 SH_CLERK         64300  
  258.            50 ST_CLERK         55700  
  259.            50                 156400  
  260.                               211200  
  261.   
  262. Example of a ROLLUP Operator   
  263.   
  264.    In the example in the slide:   
  265.     :: Total salaries for every job ID within a department for those departments whose department ID is less than 60 are displayed by the GROUP BY clause (labeled 1)  
  266. 1.选出条件department_id<60的,并且按GROUP BY子句显示.  
  267.   
  268.            10 AD_ASST           4400  
  269.   
  270.            20 MK_MAN           13000  
  271.            20 MK_REP            6000  
  272.   
  273.            30 PU_MAN           11000  
  274.            30 PU_CLERK         13900  
  275.   
  276.            40 HR_REP            6500  
  277.   
  278.            50 ST_MAN           36400  
  279.            50 SH_CLERK         64300  
  280.            50 ST_CLERK         55700  
  281.   
  282.   
  283.     :: The ROLLUP operator displays:  
  284.     -Total salary for those departments whose department ID is less than 60 (labeled     2)显示每组部门ID<60的总的salary.  
  285.   
  286.            10                   4400  
  287.   
  288.            20                  19000  
  289.   
  290.            30                  24900  
  291.   
  292.            40                   6500  
  293.   
  294.            50                 156400  
  295.   
  296.     -Total salary for all departments whose department ID is less than 60,   irrespective of the job IDs (labeled 3)(不考虑JOB_ID 号的总的salary).  
  297.   
  298.                               211200  
  299.   
  300.   
  301.     :: All rows indicated as 1 are regular rows and all rows indicated as 2 and 3 are superaggregate rows.所有行将被显示:第一步是一般的行,在第二,三步将显示聚集行.  
  302.   
  303.   The ROLLUP operator creates subtotals that roll up from the most detailed level to a grand total, following the grouping list specified in the GROUP BY clause. First it calculates the standard aggregate values for the groups specified in the GROUP BY clause (in the example, the sum of salaries grouped on each job within a department). Then it creates progressively higher-level subtotals, moving from right to left through the list of grouping columns. (In the preceding example, the sum of salaries for each department is calculated, followed by the sum of salaries for all departments.)  
  304.     
  305. /./././././  
  306.    :: Given n expressions in the ROLLUP operator of the GROUP BY clause, the operation results in n + 1 = 2 + 1 = 3 groupings.   
  307.    :: Rows based on the values of the first n expressions are called rows or regular rows and the others are called superaggregate rows.   
  308.   
  309.   
  310.                 --CUBE操作  
  311.   
  312. SELECT      [column,] group_function(column)...  
  313. FROM        table  
  314. [WHERE      condition]  
  315. [GROUP BY   [CUBE] group_by_expression]  
  316. [HAVING     having_expression]  
  317. [ORDER BY   column];  
  318.   
  319. :: CUBE 是 GROUP BY 子句的扩展  
  320. :: 能够用 CUBE 操作产生带单个 SELECT 语句的交叉表值  
  321.   
  322. The CUBE Operator   
  323.      The CUBE operator is an additional switch in the GROUP BY clause in a SELECT statement. The CUBE operator can be applied to all aggregate functions(能够用于所有聚集函数), including AVGSUMMAXMINand COUNT. It is used to produce results sets that are typically used for cross-tabular reports. While ROLLUP produces only a fraction of possible subtotal combinations, CUBE produces subtotals for all possible combinations of groupings specified in the GROUP BY clause, and a grand total.   
  324.     
  325.      The CUBE operator is used with an aggregate function to generate additional rows in a results set. Columns included in the GROUP BY clause are cross-referenced to produce a superset of groups. The aggregate function specified in the select list is applied to these groups to produce summary values for the additional superaggregate rows. The number of extra groups in the results set is determined by the number of columns included in the GROUP BY clause.  
  326.   
  327.      In fact, every possible combination of the columns or expressions in the GROUP BY clause is used to produce superaggregates.   
  328.   If you have n columns or expressions in the GROUP BY clause, there will be 2n possible superaggregate combinations. Mathematically, these combinations form an n-dimensional cube, which is how the operator got its name.  
  329.   
  330.      By using application or programming tools, these superaggregate values can then be fed into charts and graphs that convey results and relationships visually and effectively.  
  331.   
  332.   
  333.             --CUBE 操作: 例子  
  334.   
  335. SQL> select department_id,job_id,sum(salary)  
  336.   2  from employees  
  337.   3  where department_id<60  
  338.   4  group by cube(department_id,job_id)  
  339.   5  order by department_id;  
  340.   
  341. DEPARTMENT_ID JOB_ID     SUM(SALARY)  
  342. ------------- ---------- -----------  
  343.            10 AD_ASST           4400  
  344.            10                   4400  
  345.            20 MK_MAN           13000  
  346.            20 MK_REP            6000  
  347.            20                  19000  
  348.            30 PU_CLERK         13900  
  349.            30 PU_MAN           11000  
  350.            30                  24900  
  351.            40 HR_REP            6500  
  352.            40                   6500  
  353.            50 SH_CLERK         64300  
  354.            50 ST_CLERK         55700  
  355.            50 ST_MAN           36400  
  356.            50                 156400  
  357.               AD_ASST           4400  
  358.               HR_REP            6500  
  359.               MK_MAN           13000  
  360.               MK_REP            6000  
  361.               PU_CLERK         13900  
  362.               PU_MAN           11000  
  363.               SH_CLERK         64300  
  364.               ST_CLERK         55700  
  365.               ST_MAN           36400  
  366.                               211200  
  367.   
  368. Example of a CUBE Operator   
  369.   
  370.    The output of the SELECT statement in the example can be interpreted as follows:  
  371.   
  372.    :: The total salary for every job within a department (for those departments whose department ID is less than 60) is displayed by the GROUP BY clause (labeled 1)  
  373.   
  374.            10 AD_ASST           4400  
  375.   
  376.            20 MK_MAN           13000  
  377.            20 MK_REP            6000  
  378.   
  379.            30 PU_MAN           11000  
  380.            30 PU_CLERK         13900  
  381.   
  382.            40 HR_REP            6500  
  383.   
  384.            50 ST_MAN           36400  
  385.            50 SH_CLERK         64300  
  386.            50 ST_CLERK         55700  
  387.   
  388.    :: The total salary for those departments whose department ID is less than 60 (labeled       2)  
  389.            10                   4400  
  390.   
  391.            20                  19000  
  392.   
  393.            30                  24900  
  394.   
  395.            40                   6500  
  396.   
  397.            50                 156400  
  398.   
  399.    :: The total salary for every job irrespective of the department (labeled 3)  
  400. (每个job无关的department的总的salary)  
  401.   
  402.               HR_REP            6500  
  403.               MK_MAN           13000  
  404.               MK_REP            6000  
  405.               PU_MAN           11000  
  406.               ST_MAN           36400  
  407.               AD_ASST           4400  
  408.               PU_CLERK         13900  
  409.               SH_CLERK         64300  
  410.               ST_CLERK         55700  
  411.   
  412.    :: Total salary for those departments whose department ID is less than 60,       irrespective of the job titles (labeled 4)  
  413.   
  414.                               211200  
  415.   
  416.    In the preceding example, all rows indicated as 1 are regular rowsall rows indicated as 2 and 4 are superaggregate rowsand all rows indicated as 3 are cross-tabulation values.  
  417.    The CUBE operator has also performed the ROLLUP operation to display the subtotals for those departments whose department ID is less than 60 and the total salary for those departments whose department ID is less than 60, irrespective of the job titles. Additionally, the CUBE operator displays the total salary for every job irrespective of the department.   
  418.    
  419.   Note: Similar to the ROLLUP operator, producing subtotals in n dimensions (that is, n columns in the GROUP BY clause) without a CUBE operator requires 2n SELECT statements to be linked with UNION ALL. Thus, a report with three dimensions requires 2的3次方= 8 SELECT statements to be linked with UNION ALL.  
  420.   
  421.   
  422.   
  423.             --GROUPING 函数  
  424.   
  425. SELECT    [column,] group_function(column) . ,  
  426.       GROUPING(expr)  
  427. FROM      table  
  428. [WHERE    condition]  
  429. [GROUP BY [ROLLUP][CUBE] group_by_expression]  
  430. [HAVING   having_expression]  
  431. [ORDER BY column];  
  432.   
  433. :: GROUPING 函数既能够被用于 CUBE 操作,也可以被用于 ROLLUP 操作  
  434. :: 用 GROUPING 函数模拟能够发现在一行中的构成小计的分组  
  435. :: 用 GROUPING 函数,你能够从 ROLLUP 或 CUBE 创建的空值中区分存储的 NULL 值  
  436. :: GROUPING 函数返回 0 或 1  
  437.   
  438. The GROUPING Function   
  439.   
  440.    The GROUPING function can be used with either the CUBE or ROLLUP operator to help you understand how a summary value has been obtained.   
  441.      
  442. ././././.  
  443.    The GROUPING function uses a single column as its argument(使用单一列). The expr(表达式) in the GROUPING function must match one of the expressions in the GROUP BY clause. The function returns a value of 0 or 1.  
  444.     
  445.   The values returned by the GROUPING function(被这个函数返回的值经常用于) are useful to:  
  446.     :: Determine the level of aggregation of a given subtotal; that is, the group or           groups on which the subtotal is based//确定组的小计值  
  447.     :: Identify whether a NULL value in the expression column of a row of the result set indicates:     ././././././识别在结果集的列的表达式列中NULL值的说明:  
  448.         -A NULL value from the base table (stored NULL value)  
  449.         -A NULL value created by ROLLUP/CUBE (as a result of a group function on that        expression)  
  450.   
  451.    ././././././  
  452.    A value of 0 returned by the GROUPING function based on an expression indicates one of the following:  
  453.         :: The expression has been used to calculate the aggregate value.  
  454.         表达式已计算出聚集值.  
  455.         :: The NULL value in the expression column is a stored NULL value.  
  456.            表达式中的NULL值是存储的NULL值  
  457.    A value of 1 returned by the GROUPING function based on an expression indicates one of the following:   
  458.         :: The expression has not been used to calculate the aggregate value.   
  459.         :: The NULL value in the expression column is created by ROLLUP or CUBE as a           result of grouping.  
  460.   
  461.   
  462.   
  463.             --GROUPING 函数: 例子  
  464.   
  465. SELECT   department_id DEPTID, job_id JOB,   
  466.          SUM(salary),  
  467.          GROUPING(department_id) GRP_DEPT,  
  468.          GROUPING(job_id) GRP_JOB  
  469. FROM     employees  
  470. WHERE    department_id < 50  
  471. GROUP BY ROLLUP(department_id, job_id);  
  472.   
  473.     DEPTID JOB        SUM(SALARY)   GRP_DEPT    GRP_JOB  
  474. ---------- ---------- ----------- ---------- ----------  
  475.         10 AD_ASST           4400          0          0 //  
  476.         10                   4400          0          1 //  
  477.         20 MK_MAN           13000          0          0  
  478.         20 MK_REP            6000          0          0  
  479.         20                  19000          0          1  
  480.         30 PU_MAN           11000          0          0  
  481.         30 PU_CLERK         13900          0          0  
  482.         30                  24900          0          1  
  483.         40 HR_REP            6500          0          0  
  484.         40                   6500          0          1  
  485.                             54800          1          1 //  
  486.   
  487. SQL> SELECT   department_id DEPTID, job_id JOB,  
  488.   2           SUM(salary),  
  489.   3           GROUPING(department_id) GRP_DEPT  
  490.   4   FROM     employees  
  491.   5   WHERE    department_id < 50  
  492.   6   GROUP BY ROLLUP(department_id, job_id);  
  493.   
  494.     DEPTID JOB        SUM(SALARY)   GRP_DEPT  
  495. ---------- ---------- ----------- ----------  
  496.         10 AD_ASST           4400          0  
  497.         10                   4400          0  
  498.         20 MK_MAN           13000          0  
  499.         20 MK_REP            6000          0  
  500.         20                  19000          0  
  501.         30 PU_MAN           11000          0  
  502.         30 PU_CLERK         13900          0  
  503.         30                  24900          0  
  504.         40 HR_REP            6500          0  
  505.         40                   6500          0  
  506.                             54800          1  
  507.   
  508. Example of a GROUPING Function   
  509.   
  510.    In the example in the slide, consider the summary value 4400 in the first row (labeled 1). This summary value is the total salary for the job ID of AD_ASST within department 10. To calculate this summary value, both the columns DEPARTMENT_ID and JOB_ID have been taken into account. Thus a value of 0 is returned for both the expressions GROUPING(department_id) and GROUPING(job_id).  
  511.   
  512.    Consider the summary value 4400 in the second row (labeled 2). This value is the total salary for department 10 and has been calculated by taking into account the column DEPARTMENT_ID; thus a value of 0 has been returned by GROUPING(department_id). Because the column JOB_ID has not been taken into account to calculate this value, a value of 1 has been returned for GROUPING(job_id). You can observe similar output in the fifth row.  
  513.    
  514.   In the last row, consider the summary value 23400 (labeled 3). This is the total salary for those departments whose department ID is less than 50 and all job titles. To calculate this summary value, neither of the columns DEPARTMENT_ID and JOB_ID have been taken into account. Thus a value of 1 is returned for both the expressions GROUPING(department_id) and GROUPING(job_id).  
  515.    
  516. //插的,老师讲解:  
  517.  Instructor Note   
  518.     Explain that if the same example is run with the CUBE operator, it returns a results set that has 1 for GROUPING(department_id) and 0 for GROUPING(job_id) in the cross-tabulation rows, because the subtotal values are the result of grouping on job irrespective of department number.  
  519.   
  520. SQL> SELECT   department_id DEPTID, job_id JOB,  
  521.   2           SUM(salary),  
  522.   3           GROUPING(department_id) GRP_DEPT,  
  523.   4           GROUPING(job_id) GRP_JOB  
  524.   5  FROM     employees  
  525.   6  WHERE    department_id < 50  
  526.   7  GROUP BY CUBE(department_id, job_id);  
  527.   8  ORDER BY department_id  /deptid/DEPTID/1  ;  
  528.   
  529.     DEPTID JOB        SUM(SALARY)   GRP_DEPT    GRP_JOB  
  530. ---------- ---------- ----------- ---------- ----------  
  531.         10 AD_ASST           4400          0          0  
  532.         10                   4400          0          1  
  533.         20 MK_MAN           13000          0          0  
  534.         20 MK_REP            6000          0          0  
  535.         20                  19000          0          1  
  536.         30 PU_CLERK         13900          0          0  
  537.         30 PU_MAN           11000          0          0  
  538.         30                  24900          0          1  
  539.         40 HR_REP            6500          0          0  
  540.         40                   6500          0          1  
  541.            AD_ASST           4400          1          0  
  542.            HR_REP            6500          1          0  
  543.            MK_MAN           13000          1          0  
  544.            MK_REP            6000          1          0  
  545.            PU_CLERK         13900          1          0  
  546.            PU_MAN           11000          1          0  
  547.                             54800          1          1  
  548.   
  549.   
  550.   
  551.             --分组集合  
  552.   
  553. :: GROUPING SETS 是 GROUP BY 子句更进一步的扩展  
  554. :: 你能够用 GROUPING SETS 在同一查询中定义多重分组  
  555. :: Oracle 服务器计算在 GROUPING SETS 子句中指定的所有分组,并且用 UNION ALL 操作组合单个的分组结果  
  556. :: 分组集合的效率:  
  557.     -对基表仅进行一个查询  
  558.     -不需要写复杂的 UNION 语句  
  559.     -GROUPING SETS 有更多的元素,更好的执行性能  
  560.   
  561.   
  562. GROUPING SETS  
  563.   
  564.    GROUPING SETS are a further extension of the GROUP BY clause that let you specify multiple groupings of data(可以指定多个组的数据). Doing so facilitates efficient aggregation and hence facilitates analysis of data across multiple dimensions.   
  565.      
  566.    A single SELECT statement can now be written using 'GROUPING SETS' to specify various groupings (that can also include ROLLUP or CUBE operators), rather than multiple SELECT statements combined by UNION ALL operators. For example, you can say:  
  567.   
  568.    SELECT   department_id, job_id, manager_id, AVG(salary)  
  569.    FROM     employees  
  570.    GROUP BY   
  571.    GROUPING SETS  
  572.    ((department_id, job_id, manager_id),  
  573.    (department_id, manager_id),(job_id, manager_id));   
  574. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  575. ------------- ---------- ---------- -----------  
  576.               SA_REP            149        7000  
  577.            10 AD_ASST           101        4400  
  578.            20 MK_MAN            100       13000  
  579.            20 MK_REP            201        6000  
  580.            30 PU_MAN            100       11000  
  581.            30 PU_CLERK          114        2780  
  582.            40 HR_REP            101        6500  
  583.            50 ST_MAN            100        7280  
  584.            50 SH_CLERK          120        2900  
  585.            50 ST_CLERK          120        2625  
  586.            50 SH_CLERK          121        3675  
  587.   
  588. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  589. ------------- ---------- ---------- -----------  
  590.            50 ST_CLERK          121        2675  
  591.            50 SH_CLERK          122        3200  
  592.            50 ST_CLERK          122        2700  
  593.            50 SH_CLERK          123        3475  
  594.            50 ST_CLERK          123        3000  
  595.            50 SH_CLERK          124        2825  
  596.            50 ST_CLERK          124        2925  
  597.            60 IT_PROG           102        9000  
  598.            60 IT_PROG           103        4950  
  599.            70 PR_REP            101       10000  
  600.            80 SA_MAN            100       12200  
  601.   
  602. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  603. ------------- ---------- ---------- -----------  
  604.            80 SA_REP            145        8500  
  605.            80 SA_REP            146        8500  
  606.            80 SA_REP            147  7766.66667  
  607.            80 SA_REP            148        8650  
  608.            80 SA_REP            149        8600  
  609.            90 AD_PRES                     24000  
  610.            90 AD_VP             100       17000  
  611.           100 FI_MGR            101       12000  
  612.           100 FI_ACCOUNT        108        7920  
  613.           110 AC_MGR            101       12000  
  614.           110 AC_ACCOUNT        205        8300  
  615.   
  616. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  617. ------------- ---------- ---------- -----------  
  618.                                 149        7000  
  619.            10                   101        4400  
  620.            20                   100       13000  
  621.            20                   201        6000  
  622.            30                   100       11000  
  623.            30                   114        2780  
  624.            40                   101        6500  
  625.            50                   100        7280  
  626.            50                   120      2762.5  
  627.            50                   121        3175  
  628.            50                   122        2950  
  629.   
  630. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  631. ------------- ---------- ---------- -----------  
  632.            50                   123      3237.5  
  633.            50                   124        2875  
  634.            60                   102        9000  
  635.            60                   103        4950  
  636.            70                   101       10000  
  637.            80                   100       12200  
  638.            80                   145        8500  
  639.            80                   146        8500  
  640.            80                   147  7766.66667  
  641.            80                   148        8650  
  642.            80                   149        8600  
  643.   
  644. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  645. ------------- ---------- ---------- -----------  
  646.            90                             24000  
  647.            90                   100       17000  
  648.           100                   101       12000  
  649.           100                   108        7920  
  650.           110                   101       12000  
  651.           110                   205        8300  
  652.               AD_VP             100       17000  
  653.               AC_MGR            101       12000  
  654.               FI_MGR            101       12000  
  655.               HR_REP            101        6500  
  656.               MK_MAN            100       13000  
  657.   
  658. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  659. ------------- ---------- ---------- -----------  
  660.               MK_REP            201        6000  
  661.               PR_REP            101       10000  
  662.               PU_MAN            100       11000  
  663.               SA_MAN            100       12200  
  664.               SA_REP            145        8500  
  665.               SA_REP            146        8500  
  666.               SA_REP            147  7766.66667  
  667.               SA_REP            148        8650  
  668.               SA_REP            149  8333.33333  
  669.               ST_MAN            100        7280  
  670.               AD_ASST           101        4400  
  671.   
  672. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  673. ------------- ---------- ---------- -----------  
  674.               AD_PRES                     24000  
  675.               IT_PROG           102        9000  
  676.               IT_PROG           103        4950  
  677.               PU_CLERK          114        2780  
  678.               SH_CLERK          120        2900  
  679.               SH_CLERK          121        3675  
  680.               SH_CLERK          122        3200  
  681.               SH_CLERK          123        3475  
  682.               SH_CLERK          124        2825  
  683.               ST_CLERK          120        2625  
  684.               ST_CLERK          121        2675  
  685.   
  686. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  687. ------------- ---------- ---------- -----------  
  688.               ST_CLERK          122        2700  
  689.               ST_CLERK          123        3000  
  690.               ST_CLERK          124        2925  
  691.               AC_ACCOUNT        205        8300  
  692.               FI_ACCOUNT        108        7920  
  693.   
  694. ./././././  
  695.    This statement calculates aggregates over three groupings:   
  696.    (department_id, job_id, manager_id), (department_id, manager_id)   
  697.    and (job_id, manager_id)   
  698.   
  699.     Without this enhancement in Oracle9i, multiple queries combined together with UNION ALL are required to get the output of the preceding SELECT statement. A multiquery approach is inefficient, for it requires multiple scans of the same data.  
  700.   
  701.   
  702. GROUPING SETS (continued)  
  703.     
  704.   Compare the preceding statement with this alternative:   
  705.   
  706.    SELECT   department_id, job_id, manager_id, AVG(salary)  
  707.    FROM     employees  
  708.    GROUP BY CUBE(department_id, job_id, manager_id);  
  709.   
  710.   The preceding statement computes all the 8 (2 *2 *2) groupings, though only the groups      (department_id, job_id, manager_id), (department_id, manager_id) and (job_id, manager_id)are of interest to you.  
  711.   
  712.   
  713.    SELECT   department_id, job_id, manager_id, AVG(salary)  
  714.    FROM     employees  
  715.    GROUP BY                     //移过来.  
  716.    GROUPING SETS  
  717.    ((department_id, job_id, manager_id),  
  718.    (department_id, manager_id),(job_id, manager_id));   
  719.   
  720.   
  721.   Another alternative is the following statement: //结果与上面的一样  
  722.   
  723.    SELECT   department_id, job_id, manager_id, AVG(salary)  
  724.    FROM     employees  
  725.    GROUP BY department_id, job_id, manager_id   
  726.    UNION ALL  
  727.    SELECT   department_id, NULL, manager_id, AVG(salary)  
  728.    FROM     employees  
  729.    GROUP BY department_id, manager_id  
  730.    UNION ALL  
  731.    SELECT   NULL, job_id, manager_id, AVG(salary)  
  732.    FROM     employees  
  733.    GROUP BY job_id, manager_id;  
  734.   
  735.    
  736.   This statement requires three scans of the base table, making it inefficient.   
  737.   
  738.   CUBE and ROLLUP can be thought of as grouping sets with very specific semantics. The following equivalencies show this fact:   
  739.   
  740. CUBE (a,b,c)        GROUPING SETS       //2的3次方  
  741. is equivalent to    ((a,b,c),(a,b),(a,c),(b,c),(a),(b),(c)())  
  742.   
  743. ROLLUP(a,b,c)       GROUPING SETS  
  744. is equivalent to    ((a,b,c),(a,b),(a),())  
  745.   
  746.   
  747.   
  748.             --GROUPING SETS:例子  
  749.   
  750. SELECT   department_id, job_id,   
  751.          manager_id,avg(salary)  
  752. FROM     employees  
  753. GROUP BY GROUPING SETS   
  754. ((department_id,job_id), (job_id,manager_id));     
  755.   
  756. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  757. ------------- ---------- ---------- -----------  
  758.               SA_REP                       7000  
  759.            10 AD_ASST                      4400  
  760.            20 MK_MAN                      13000  
  761.            20 MK_REP                       6000  
  762.            30 PU_MAN                      11000  
  763.            30 PU_CLERK                     2780  
  764.            40 HR_REP                       6500  
  765.            50 ST_MAN                       7280  
  766.            50 SH_CLERK                     3215  
  767.            50 ST_CLERK                     2785  
  768.            60 IT_PROG                      5760  
  769.            70 PR_REP                      10000  
  770.            80 SA_MAN                      12200  
  771.            80 SA_REP                 8396.55172  
  772.            90 AD_VP                       17000  
  773.            90 AD_PRES                     24000  
  774.           100 FI_MGR                      12000  
  775.           100 FI_ACCOUNT                   7920  
  776.           110 AC_MGR                      12000  
  777.           110 AC_ACCOUNT                   8300  
  778.               AD_VP             100       17000  
  779.               AC_MGR            101       12000  
  780.               FI_MGR            101       12000  
  781.               HR_REP            101        6500  
  782.               MK_MAN            100       13000  
  783.               MK_REP            201        6000  
  784.               PR_REP            101       10000  
  785.               PU_MAN            100       11000  
  786.               SA_MAN            100       12200  
  787.               SA_REP            145        8500  
  788.               SA_REP            146        8500  
  789.               SA_REP            147  7766.66667  
  790.               SA_REP            148        8650  
  791. ...  
  792.   
  793. GROUPING SETS: Example  
  794.   
  795.    The query in the slide calculates aggregates over two groupings. The table is divided into the following groups:  
  796.     -Department ID, Job ID  
  797.     -Job ID, Manager ID   
  798. //每个组的平均salary要被计算.  
  799.    The average salaries for each of these groups are calculated. The results set displays average salary for each of the two groups.  
  800.   
  801.     In the output, the group marked as 1 can be interpreted as:(标记为1的说明)  
  802.     :: The average salary of all employees with the job ID AD_ASST in the department 10 is 4400.  
  803.     :: The average salary of all employees with the job ID MK_MAN in the department 20 is 13000.  
  804.     :: The average salary of all employees with the job ID MK_REP in the department 20 is 6000.  
  805.     :: The average salary of all employees with the job ID ST_CLERK in the department  50 is 2925 and so on.  
  806.   
  807. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  808.            10 AD_ASST                      4400  
  809.            20 MK_MAN                      13000     1   
  810.            20 MK_REP                       6000  
  811.        50 ST_CLERK             2925  
  812.   
  813. ...  
  814. ...  
  815.   
  816. GROUPING SETS: Example (continued)  
  817.   
  818.    The group marked as 2 in the output is interpreted as:  
  819.        :: The average salary of all employees with the job ID FI_MGR, who report to the manager with the manager ID 101, is 12000.  
  820.        :: The average salary of all employees with the job ID HR_REP, who report to the manager with the manager ID 101, is 6500, and so on.   
  821.   
  822. DEPARTMENT_ID JOB_ID     MANAGER_ID AVG(SALARY)  
  823. ------------- ---------- ---------- -----------  
  824.               FI_MGR            101       12000     2  
  825.               HR_REP            101        6500  
  826.   
  827.   
  828.    The example in the slide can also be written as:  
  829.   
  830.    SELECT   department_id, job_id, NULL as manager_id,   
  831.             AVG(salary) as AVGSAL  
  832.    FROM     employees  
  833.    GROUP BY department_id, job_id  
  834.    UNION ALL  
  835.    SELECT   NULL, job_id, manager_id, avg(salary) as AVGSAL  
  836.    FROM     employees  
  837.    GROUP BY job_id, manager_id;  
  838.   
  839.      In the absence of an optimizer that looks across query blocks to generate the execution plan, the preceding query would need two scans of the base table, EMPLOYEES. This could be very inefficient. Hence the usage of the GROUPING SETS statement is recommended.   
  840.   
  841.   
  842.   
  843.                 复合列  
  844.   
  845. 复合列是一个作为整体被处理的列集合  
  846. ROLLUP (a, (b,c), d)  
  847.   
  848. :: 为了指定复合列,用 GROUP BY 子句来分组在圆括号内的列,因此, Oracle 服务器在进行 ROLLUP 或 CUBE 操作时将它们作为一个整体来处理  
  849.   
  850. ::当使用 ROLLUP 或 CUBE 时,复合列将跳过在确定级别上的集合  ././././.  
  851.   
  852. Composite Columns   
  853.    
  854.     A composite column is a collection of columns that are treated as a unit during the computation of groupings. You specify the columns in parentheses(括号) as in the following statement:   
  855.    ROLLUP (a, (b, c), d)    (b,c)就是一个复合列,并且做为一个单元处理.  
  856.    
  857. /././././././././././.  
  858.    Here, (b,c) form a composite column and are treated as a unit. In general, composite columns are useful in ROLLUPCUBEand GROUPING SETS. For example, in CUBE or ROLLUP, composite columns would mean skipping aggregation across certain levels.  
  859.   
  860.  That is,   GROUP BY ROLLUP(a, (b, c))  
  861.   
  862.  is equivalent to                 
  863.    GROUP BY a, b, c UNION ALL  
  864.    GROUP BY a UNION ALL  
  865.    GROUP BY ()  
  866.   
  867.  Here, (b, c) are treated as a unit and rollup will not be applied across (b, c). It is as if you have an alias, for example z, for (b, c), and the GROUP BY expression reduces to   
  868. GROUP BY ROLLUP(a, z).   
  869.   
  870. Note: GROUP BY( ) is typically a SELECT statement with NULL values for the columns a and b and only the aggregate function. This is generally used for generating the  grand totals.  
  871.    SELECT   NULLNULL, aggregate_col  
  872.    FROM     <table_name>  
  873.    GROUP BY ( );  
  874.   
  875.   
  876. (continued)  
  877.   
  878.  Compare this with the normal ROLLUP as in:   
  879.    GROUP BY ROLLUP(a, b, c)  
  880.   
  881.  which would be   
  882.    GROUP BY a, b, c UNION ALL         
  883.    GROUP BY a, b UNION ALL  
  884.    GROUP BY a UNION ALL  
  885.    GROUP BY ().  
  886.   
  887.  Similarly,   
  888.    GROUP BY CUBE((a, b), c)   
  889.   
  890.  would be equivalent to           
  891.    GROUP BY a, b, c UNION ALL  
  892.    GROUP BY a, b UNION ALL  
  893.    GROUP BY c UNION ALL  
  894.    GROUP By ()  
  895.   
  896.  The following table shows grouping sets specification and equivalent GROUP BY specification.  
  897.   
  898. GROUPING SETS Satements             Equivalent GROUP BY Statements  
  899.   
  900.                         GROUP BY a UNION ALL  
  901. GROUP BY GROUPING SETS(a,b,c)           GROUP BY b UNION ALL  
  902.                         GROUP BY c  
  903.   
  904. GROUP BY GROUPING SETS(a,b,(b,c))       GROUP BY a UNION ALL  
  905. (The GROUPING SETS expression has a     GROUP BY b UNION ALL  
  906. composite column)               GROUP BY b,c  
  907.   
  908. GROUP BY GROUPING SETS((a,b,c))         GROUP BY a,b,c  
  909.   
  910.                         GROUP BY a UNION ALL  
  911. GROUP BY GROUPING SETS(a,(b),())        GROUP BY b UNION ALL  
  912.                         GROPY BY ()  
  913.   
  914. GROUP BY GROUPING SETS(a,ROLLUP(b,c))       GROUP BY a UNION ALL  
  915. (The GROUPING SETS expression has a     GROUP BY ROLLUP(b,c)  
  916. composite column)  
  917.   
  918.   
  919.             复合列:例子  
  920.   
  921. SELECT   department_id, job_id, manager_id, SUM(salary)  
  922. FROM     employees    
  923. GROUP BY ROLLUP( department_id,(job_id, manager_id));  
  924. 把(job_id, manager_id)看成一个单元处理.  
  925.   
  926. grouping by :  
  927. 1.  (department_id,job_id,manager_id)  
  928. 2.  (department_id)  
  929. 3.  ( )  
  930.   
  931.   
  932.   
  933. Composite Columns: Example  
  934.   
  935. Consider the example:   
  936.    SELECT department_id, job_id,manager_id, SUM(salary)  
  937.    FROM   employees    
  938.    GROUP BY ROLLUP( department_id,job_id, manager_id);  
  939.   
  940. The preceding query results in the Oracle Server computing the following groupings:  
  941. 1.  (department_id, job_id, manager_id)  
  942. 2.  (department_id, job_id)  
  943. 3.  (department_id)  
  944. 4.  ( )     //所有的sum(salary)  
  945.   
  946.   
  947. DEPARTMENT_ID JOB_ID     MANAGER_ID SUM(SALARY)  
  948. ------------- ---------- ---------- -----------  
  949.            10 AD_ASST           101        4400  
  950.            10 AD_ASST                      4400  
  951.            10                              4400  
  952.            20 MK_MAN            100       13000  
  953.            20 MK_MAN                      13000  
  954.            20 MK_REP            201        6000  
  955.            20 MK_REP                       6000  
  956.            20                             19000  
  957.            30 PU_MAN            100       11000  
  958.            30 PU_MAN                      11000  
  959.            30 PU_CLERK          114       13900  
  960.            30 PU_CLERK                    13900  
  961.            30                             24900  
  962.            50 ST_MAN            100       36400  
  963.            50 ST_MAN                      36400  
  964.            50 SH_CLERK          120       11600  
  965.            50 SH_CLERK          121       14700  
  966.            50 SH_CLERK          122       12800  
  967.            50 SH_CLERK          123       13900  
  968.            50 SH_CLERK          124       11300  
  969.            50 SH_CLERK                    64300  
  970.            50 ST_CLERK          120       10500  
  971.            50 ST_CLERK          121       10700  
  972.            50 ST_CLERK          122       10800  
  973.            50 ST_CLERK          123       12000  
  974.            50 ST_CLERK          124       11700  
  975.            50 ST_CLERK                    55700  
  976.            50                            156400  
  977. ...  
  978. ...  
  979.                                          691400  
  980.   
  981.    If you are just interested in grouping of lines (1), (3), and (4) in the preceding example, you cannot limit the calculation to  those groupings without using composite columns. With composite columns, this is possible by treating JOB_ID and MANAGER_ID columns  as a single unit while rolling up. Columns enclosed in parentheses are treated as a unit while computing ROLLUP and CUBE. This is illustrated in the example on the slide. By enclosing JOB_ID and MANAGER_ID columns in parenthesis, we indicate to the Oracle Server to treat JOB_ID and MANAGER_ID as a single unit, as a composite column.   
  982.   
  983.   
  984.   
  985. (continued)  
  986.   
  987.   The example in the slide computes the following groupings:  
  988.     : (department_id, job_id, manager_id)  
  989.     : (department_id)  
  990.     :( )  
  991.   
  992.   The example in the slide displays the following:  
  993.     : Total salary for every department (labeled 1)  
  994.     : Total salary for every department, job ID, and manager (labeled 2)  
  995.     : Grand total (labeled 3)  
  996.   
  997.   The example in the slide can also be written as:  
  998.   SELECT   department_id, job_id, manager_id, SUM(salary)  
  999.   FROM     employees    
  1000.   GROUP BY department_id,job_id, manager_id  
  1001.   UNION  ALL  
  1002.   SELECT   department_id, TO_CHAR(NULL),TO_NUMBER(NULL), SUM(salary)  
  1003.   FROM     employees    
  1004.   GROUP BY department_id  
  1005.   UNION ALL  
  1006.   SELECT   TO_NUMBER(NULL), TO_CHAR(NULL),TO_NUMBER(NULL), SUM(salary)  
  1007.   FROM     employees    
  1008.   GROUP BY ();  
  1009.   
  1010.   In the absence of an optimizer that looks across query blocks to generate the execution plan, the preceding query would need three scans of the base table, EMPLOYEES. This could be very inefficient. Hence, the use of composite columns is recommended.  
  1011.   
  1012.   
  1013. SQL>   SELECT   TO_NUMBER(NULL), TO_CHAR(NULL),TO_NUMBER(NULL), SUM(salary)  
  1014.   2    FROM     employees  
  1015.   3    GROUP BY ();  
  1016.   
  1017. TO_NUMBER(NULL) T TO_NUMBER(NULLSUM(SALARY)  
  1018. --------------- - --------------- -----------  
  1019.                                        691400  
  1020.   
  1021. SQL> select count(*),sum(salary)  
  1022.   2  from employees;  
  1023.   
  1024.   COUNT(*) SUM(SALARY)  
  1025. ---------- -----------  
  1026.        107      691400  
  1027.   
  1028. SQL> select null,null,null,sum(salary)  
  1029.   2  from employees;  
  1030.   
  1031. N N N SUM(SALARY)  
  1032. - - - -----------  
  1033.            691400  
  1034.   
  1035. SQL> select null,null,null,sum(salary)  
  1036.   2  FROM     employees  
  1037.   3  GROUP BY ();  
  1038.   
  1039. N N N SUM(SALARY)  
  1040. - - - -----------  
  1041.            691400  
  1042.   
  1043.   
  1044.   
  1045.             连接分组  
  1046.   
  1047.   
  1048. :: 连接分组提供一种简明的方式来生成有用的分组组合  
  1049. :: 为了指定连接分组集合,用逗号分开多重分组集合, ROLLUP,和 CUBE 操作,以便 Oracle 服务器将它们组合在一个单个的 GROUP BY 子句中  
  1050. :: 分组是每个分组集合交叉乘积的结果  
  1051.   
  1052.   GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)  
  1053.   
  1054.   
  1055. Concatenated Columns    
  1056.   
  1057.   Concatenated groupings offer a concise way to generate useful combinations of groupings. The concatenated groupings are specified simply by listing multiple grouping sets, cubes, and rollups, and separating them with commas. Here is an example of concatenated grouping sets:   
  1058.   
  1059.   
  1060. GROUP BY GROUPING SETS(a, b), GROUPING SETS(c, d)  
  1061.    
  1062.   The preceding SQL defines the following groupings:   
  1063. (a, c), (a, d), (b, c), (b, d)  
  1064.   
  1065.   
  1066.   Concatenation of grouping sets is very helpful for these reasons:   
  1067.     :: Ease of query development: you need not manually enumerate all groupings  
  1068.         //不用手动列举所有的组.  
  1069.     :: Use by applications: SQL generated by OLAP applications often involves concatenation of grouping sets, with each grouping set defining groupings needed for a dimension   
  1070.   
  1071.   
  1072.   
  1073.             --连接分组例子  
  1074.   
  1075. SELECT   department_id, job_id, manager_id,   
  1076.          SUM(salary)  
  1077. FROM     employees  
  1078. GROUP BY department_id,ROLLUP(job_id),CUBE(manager_id);  
  1079.   
  1080. DEPARTMENT_ID JOB_ID     MANAGER_ID SUM(SALARY)  
  1081. ------------- ---------- ---------- -----------  
  1082.               SA_REP            149        7000  
  1083.            10 AD_ASST           101        4400  
  1084.            20 MK_MAN            100       13000  
  1085.            20 MK_REP            201        6000  
  1086.            30 PU_MAN            100       11000  
  1087. ...  
  1088.            80 SA_MAN            100       61000  
  1089.           100 FI_MGR            101       12000  
  1090.           100 FI_ACCOUNT        108       39600  
  1091.           110 AC_MGR            101       12000  
  1092.           110 AC_ACCOUNT        205        8300  
  1093. ...  
  1094.                                 149        7000  
  1095.            10                   101        4400  
  1096.            20                   100       13000  
  1097.            20                   201        6000  
  1098.            30                   100       11000  
  1099.            30                   114       13900  
  1100.            50                   123       25900  
  1101.            50                   124       23000  
  1102.            60                   102        9000  
  1103.            60                   103       19800  
  1104. ...  
  1105.           100                   101       12000  
  1106.           100                   108       39600  
  1107.           110                   101       12000  
  1108.           110                   205        8300  
  1109.               SA_REP                       7000  
  1110.                                            7000  
  1111.            10 AD_ASST                      4400  
  1112.            10                              4400  
  1113.            20 MK_MAN                      13000  
  1114.            20 MK_REP                       6000  
  1115.            20                             19000  
  1116. ...  
  1117.            50 ST_MAN                      36400  
  1118.            50 SH_CLERK                    64300  
  1119.            50 ST_CLERK                    55700  
  1120.            50                            156400  
  1121.            60 IT_PROG                     28800  
  1122.            60                             28800  
  1123.            70 PR_REP                      10000  
  1124.            70                             10000  
  1125. ...  
  1126.           100 FI_ACCOUNT                  39600  
  1127.           100                             51600  
  1128.           110 AC_MGR                      12000  
  1129.           110 AC_ACCOUNT                   8300  
  1130.           110                             20300  
  1131.   
  1132. Concatenated Groupings Example  
  1133.   
  1134.  The example in the slide results in the following groupings:  
  1135.     -(department_id, manager_id, job_id )  
  1136.     -(department_id, manager_id)  
  1137.     -(department_id, job_id)  
  1138.     -(department_id)  
  1139.   
  1140.   The total salary for each of these groups is calculated.  
  1141.   
  1142.   The example in the slide displays the following:  
  1143.      :: Total salary for every department, job ID, manager  
  1144.      :: Total salary for every  department, manager ID  
  1145.      :: Total salary for every  department, job ID  
  1146.      :: Total salary for every department   
  1147.   
  1148. For easier understanding, the details for the department 10 are highlighted in the output.   
  1149.   
  1150.   
  1151.             SUMMARY  
  1152.   
  1153.  在本课中, 您应该已经学会如何:  
  1154. :: 用 ROLLUP 操作产生小计值  
  1155. :: 用 CUBE 操作产生交叉表值  
  1156. :: 用 GROUPING 函数指定由 ROLLUP 或 CUBE 创建的行值  
  1157. :: 用 GROUPING SETS 语法定义在同一查询中的多重分组  
  1158. :: 用 GROUP BY 子句以不同的方式组合表达式:  
  1159.     -组合列  
  1160.     -接分组集合  
  1161.   
  1162.   
  1163. Summary  
  1164.   
  1165.    :: ROLLUP and CUBE are extensions of the GROUP BY clause.  
  1166.    :: ROLLUP is used to display subtotal and grand total values.//每组值+总的值  
  1167.    :: CUBE is used to display cross-tabulation values.  ///交叉  
  1168.    :: The GROUPING function helps you determine whether a row is an aggregate produced by   a CUBE or ROLLUP operator.   
  1169.    :: With the GROUPING SETS syntax, you can define multiple groupings in the same query.   GROUP BY computes all the groupings specified and combines them with UNION ALL.   
  1170.   
  1171.    :: Within the GROUP BY clause, you can combine expressions in various ways:  
  1172.     -To specify composite columns, you group columns within parentheses so that the Oracle Server treats them as a unit while computing ROLLUP or CUBE operations.  
  1173.     -To specify concatenated grouping sets, you separate multiple grouping sets, ROLLUPand 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.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值