oracle中grouping函数,[每日一题] OCP1z0-047 :2013-08-15     描述GROUPING 函数 ................................

db8962d14114ea5d2e11a13d285261ff.png

正确答案:C

在Oracle 8i中引入GROUPING()函数,被用来做为GROUPING()函数参数的表达式必须与出现在GROUP BY子句中的表达式相匹配。包含了CUBE、ROLLUP或GROUPING 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 theGROUPING 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.

QQ:252803295

学习交流QQ群:

DSI&Core Search  Ⅰ 群:127149411(技术:已满)

DSI&Core Search  Ⅱ 群:177089463(技术:未满)

DSI&Core Search  Ⅲ 群:284596437(技术:未满)

DSI&Core Search  Ⅳ 群:192136702(技术:未满)

DSI&Core Search  Ⅴ 群:285030382(闲聊:未满)

MAIL:oracledba_cn@hotmail.com

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值