[每日一题] OCP1z0-047 :2013-08-15 描述GROUPING 函数 .......................................43




 

 正确答案:C

 

         在Oracle 8i中引入GROUPING(<列引用>)函数,被用来做为GROUPING()函数参数的表达式必须与出现在GROUP BY 子句中的表达式相匹配。包含了CUBEROLLUPGROUPING SET关键字的组查询时,该函数对<列引用>相关列的聚合结果中的NULL值进行检查。例如通过写出decode(grouping(id),1,’ALLID’,id) id来检测id是否有一行由CUBE产生的空值,或着是否其在数据库中本身就是空值。如果这些NULL值是由本次CUBE查询生成的,那么返回1,否则返回0。


  官方解释:GROUPING distinguishes superaggregate rows fromregular grouped rows. GROUP BY extensions such as ROLLUP and CUBE produce superaggregate rows where the set of all values is represented bynull. Using the GROUPING function,you can distinguish a null representing the set of all values in asuperaggregate row from a null in a regular row.

The expr in the GROUPING function must matchone of the expressions in the GROUP BY clause. The functionreturns a value of 1 if the value ofexpr in the row is a nullrepresenting the set of all values. Otherwise, it returns zero. The datatype ofthe value returned by the GROUPING function is Oracle NUMBER. Please refer to theSELECTgroup_by_clause for a discussion of theseterms.


Examples 1

In the following example, which uses the sample tables hr.departments andhr.employees, if the GROUPING function returns 1 (indicating asuperaggregate row rather than a regular row from the table), then the string"All Jobs" appears in the "JOB" column instead of the nullthat would otherwise appear:



SQL> SELECT DECODE(GROUPING(department_name), 1, 'All Departments',
  2     department_name) AS department,
  3     DECODE(GROUPING(job_id), 1, 'All Jobs', job_id) AS job,
  4     COUNT(*) "Total Empl", AVG(salary) * 12 "Average Sal"
  5     FROM employees e, departments d
  6     WHERE d.department_id = e.department_id
  7     GROUP BY ROLLUP (department_name, job_id);

DEPARTMENT                     JOB        Total Empl Average Sal
------------------------------ ---------- ---------- -----------
IT                             IT_PROG             5       69120
IT                             All Jobs            5       69120
Sales                          SA_MAN              5      146400
Sales                          SA_REP             29  100758.621
Sales                          All Jobs           34  107470.588
Finance                        FI_MGR              1      144096
Finance                        FI_ACCOUNT          5       95040
Finance                        All Jobs            6      103216
Shipping                       ST_MAN              5       87360
Shipping                       SH_CLERK           20       38580
Shipping                       ST_CLERK           20       33420

DEPARTMENT                     JOB        Total Empl Average Sal
------------------------------ ---------- ---------- -----------
Shipping                       All Jobs           45  41706.6667
Executive                      AD_VP               2      204000
Executive                      AD_PRES             1      288000
Executive                      All Jobs            3      232000
Marketing                      MK_MAN              1      156000
Marketing                      MK_REP              1       72000
Marketing                      All Jobs            2      114000
Accounting                     AC_MGR              1      144096
Accounting                     AC_ACCOUNT          1       99600
Accounting                     All Jobs            2      121848
Purchasing                     PU_MAN              1      132000

DEPARTMENT                     JOB        Total Empl Average Sal
------------------------------ ---------- ---------- -----------
Purchasing                     PU_CLERK            5       33360
Purchasing                     All Jobs            6       49800
Administration                 AD_ASST             1       52800
Administration                 All Jobs            1       52800
Human Resources                HR_REP              1       78000
Human Resources                All Jobs            1       78000
Public Relations               PR_REP              1      120000
Public Relations               All Jobs            1      120000
All Departments                All Jobs          106  77481.0566

31 rows selected.


Examples 2

 

SQL> create table gyj_test(id int,name varchar2(10));

Table created.
SQL> insert into gyj_test values(1,'A');

1 row created.

SQL> insert into gyj_test values(1,'A');

1 row created.

SQL> insert into gyj_test values(1,'A');

1 row created.

SQL> insert into gyj_test values(2,'B');

1 row created.

SQL> insert into gyj_test values(2,'B');

1 row created.

SQL> insert into gyj_test values(3,'C');

1 row created.

SQL> commit;

Commit complete.

SQL> select id,name,sum(id) sumid,grouping(id),grouping(name) from gyj_test group by rollup(id,name);

        ID NAME            SUMID GROUPING(ID) GROUPING(NAME)
---------- ---------- ---------- ------------ --------------
         1 A                   3            0              0
         1                     3            0              1
         2 B                   4            0              0
         2                     4            0              1
         3 C                   3            0              0
         3                     3            0              1
                              10            1              1

7 rows selected.


 



posted @ 2013-08-16 19:44  pangbangb  阅读(206)  评论(0编辑  收藏  举报