如何在 Oracle 中使用 Grouping 子句处理 NULL 值?
问题
您希望将 GROUPING() 与 ROLLUP、CUBE 和 SETS 函数一起使用。
解决方案
GROUPING() 函数接受一个列并返回 0 或 1。当列值为 null 时,此函数返回 1;当列值为非 null 时,返回 0。但是,GROUPING() 仅在使用 ROLLUP 或 CUBE 的查询中使用。当您希望在返回 null 的情况下显示一个值时,GROUPING() 非常有用。
ROLLUP 中的单列 GROUPING()
将列传递给 ROLLUP 的 SQL
以下示例中的最后一行将生成费用总计/总和。此总数表示为 NULL。
示例
SELECT class_id, SUM(fees) FROM students GROUP BY ROLLUP(class_id) ORDER BY class_id;
输出
class_id | SUM |
CL_MATH | 39600 |
CL_PHY | 12008 |
INS_CHAIRMAN | 24000 |
INS_STAFF | 28800 |
INS_VP | 34000 |
138408 | |
最后一行中的 class_id 列为 null。我们可以使用 GROUPING() 函数来确定此列是否为 null,如下面的查询所示。
对于具有非 null class_id 值的行,GROUPING() 返回 0;对于具有 null class_id 的最后一行,返回 1。
示例
SELECT GROUPING(class_id) As grp, class_id, SUM(fees) FROM students GROUP BY ROLLUP(class_id) ORDER BY class_id;
输出
grp | class_id | total |
0 | CL_MATH | 39600 |
0 | CL_PHY | 12008 |
0 | INS_CHAIRMAN | 24000 |
0 | INS_STAFF | 28800 |
0 | INS_VP | 34000 |
1 | | 138408 |
现在我们将使用 CASE 语句来设置有意义的值。
示例
SELECT CASE GROUPING(class_id) WHEN 1 THEN 'Total fees' ELSE class_id END AS class_group, SUM(fees) AS total FROM students GROUP BY ROLLUP(class_id) ORDER BY class_id;
输出
class_group | total |
CL_MATH | 39600 |
CL_PHY | 12008 |
INS_CHAIRMAN | 24000 |
INS_STAFF | 28800 |
INS_VP | 34000 |
总费用 | 138408 |
CUBE 中的 GROUPING()
示例
SELECT CASE GROUPING(class_id) WHEN 1 THEN 'Total Salaries' ELSE class_id END AS job_group, SUM(fees) AS total_fees FROM students GROUP BY CUBE(class_id) ORDER BY class_id;
输出
job_group | total_fees |
CL_MATH | 39600 |
CL_PHY | 12008 |
INS_CHAIRMAN | 24000 |
INS_STAFF | 28800 |
INS_VP | 34000 |
总薪资 | 138408 |
GROUPING SETS 子句通常比 CUBE 提供更好的性能。因此,您应尽可能将 GROUPING SETS 作为首选选项,而不是 CUBE。
GROUPING SETS 子句
示例
SELECT department_id, class_id, SUM(fees) AS total_fees FROM students GROUP BY GROUPING SETS(department_id, class_id) ORDER BY department_id, class_id;
输出
department_id | class_id | total_fees |
60 | | 28800 |
90 | | 34000 |
100 | | 51608 |
CL_MATH | 39600 | |
CL_PHY | 12008 | |
INS_CHAIRMAN | 24000 | |
INS_STAFF | 28800 | |
INS_VP | 34000 | |
| 24000 | |
数据准备:用于问题的數據如下所示。数据完全是为了演示目的而编造的。
示例
DROP TABLE students; COMMIT; CREATE TABLE students ( student_id NUMBER(6) , first_name VARCHAR2(20) , last_name VARCHAR2(25) CONSTRAINT stu_last_name_nn NOT NULL , email VARCHAR2(40) CONSTRAINT stu_email_nn NOT NULL , phone_number VARCHAR2(20) , join_date DATE CONSTRAINT stu_join_date_nn NOT NULL , class_id VARCHAR2(20) CONSTRAINT stu_job_nn NOT NULL , fees NUMBER(8,2) , professor_id NUMBER(6) , department_id NUMBER(4) , CONSTRAINT stu_fees_min CHECK (fees > 0) , CONSTRAINT stu_email UNIQUE (email) ) ;
示例
CREATE UNIQUE INDEX stu_id_pk ON students (student_id) ; INSERT INTO students VALUES (100,'SMITH','JAMES','[email protected]','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','[email protected]','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','[email protected]','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','[email protected]','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','[email protected]','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','[email protected]','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','[email protected]','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','[email protected]','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','[email protected]','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','[email protected]','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','[email protected]','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','[email protected]','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','[email protected]','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','[email protected]','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;