问题:
您想GROUPING()
与ROLLUP,CUBE和SETS函数一起使用。
解
该GROUPING()
函数接受一列并返回0或1。此函数在列值为null时返回1,在列值非null时返回0。但是,GROUPING()
仅在使用ROLLUP或CUBE的查询中使用。GROUPING()
当您要显示否则将返回null的值时非常有用。
SQL将列传递给ROLLUP
下例中的最后一行将得出费用的总和。该总数表示为NULL。
SELECT class_id, SUM(fees) FROM students GROUP BY ROLLUP(class_id) ORDER BY class_id;
输出结果
类号 | 和 |
CL_MATH | 39600 |
CL_PHY | 12008 |
INS_CHAIRMAN | 24000 |
INS_STAFF | 28800 |
INS_VP | 34000 |
138408 | |
最后一行的class_id列为null。WE可以使用该GROUPING()
函数确定此列是否为空,如以下查询所示。
在GROUPING()
对于具有非空值类标识码和具有空的类标识码最后一行返回1行返回0。
SELECT GROUPING(class_id) As grp, class_id, SUM(fees) FROM students GROUP BY ROLLUP(class_id) ORDER BY class_id;
输出结果
grp | 类号 | 总 |
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 | 总 |
CL_MATH | 39600 |
CL_PHY | 12008 |
INS_CHAIRMAN | 24000 |
INS_STAFF | 28800 |
INS_VP | 34000 |
总费用 | 138408 |
与多维数据集的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。
分组集子句
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 | 类号 | 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','SMITH.JAMES@notreal.com','111.111.1245',TO_DATE('17-06-2003','DD-MM-YYYY'),'INS_CHAIRMAN',24000,NULL,NULL); INSERT INTO students VALUES (101,'JOHNSON','JOHN','JOHNSON.JOHN@notreal.com','111.111.1246',TO_DATE('21-09-2005','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (102,'WILLIAMS','ROBERT','WILLIAMS.ROBERT@notreal.com','111.111.1247',TO_DATE('13-01-2001','DD-MM-YYYY'),'INS_VP',17000,100,90); INSERT INTO students VALUES (103,'BROWN','MICHAEL','BROWN.MICHAEL@notreal.com','111.111.1248',TO_DATE('03-01-2006','DD-MM-YYYY'),'INS_STAFF',9000,102,60); INSERT INTO students VALUES (104,'JONES','WILLIAM','JONES.WILLIAM@notreal.com','111.111.1249',TO_DATE('21-05-2007','DD-MM-YYYY'),'INS_STAFF',6000,103,60); INSERT INTO students VALUES (105,'MILLER','DAVID','MILLER.DAVID@notreal.com','111.111.1250',TO_DATE('25-06-2005','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (106,'DAVIS','RICHARD','DAVIS.RICHARD@notreal.com','111.111.1251',TO_DATE('05-02-2006','DD-MM-YYYY'),'INS_STAFF',4800,103,60); INSERT INTO students VALUES (107,'GARCIA','CHARLES','GARCIA.CHARLES@notreal.com','111.111.1252',TO_DATE('07-02-2007','DD-MM-YYYY'),'INS_STAFF',4200,103,60); INSERT INTO students VALUES (108,'RODRIGUEZ','JOSEPH','RODRIGUEZ.JOSEPH@notreal.com','111.111.1253',TO_DATE('17-08-2002','DD-MM-YYYY'),'CL_PHY',12008,101,100); INSERT INTO students VALUES (109,'WILSON','THOMAS','WILSON.THOMAS@notreal.com','111.111.1254',TO_DATE('16-08-2002','DD-MM-YYYY'),'CL_MATH',9000,108,100); INSERT INTO students VALUES (110,'MARTINEZ','CHRISTOPHER','MARTINEZ.CHRISTOPHER@notreal.com','111.111.1255',TO_DATE('28-09-2005','DD-MM-YYYY'),'CL_MATH',8200,108,100); INSERT INTO students VALUES (111,'ANDERSON','DANIEL','ANDERSON.DANIEL@notreal.com','111.111.1256',TO_DATE('30-09-2005','DD-MM-YYYY'),'CL_MATH',7700,108,100); INSERT INTO students VALUES (112,'TAYLOR','PAUL','TAYLOR.PAUL@notreal.com','111.111.1257',TO_DATE('07-03-2006','DD-MM-YYYY'),'CL_MATH',7800,108,100); INSERT INTO students VALUES (113,'THOMAS','MARK','THOMAS.MARK@notreal.com','111.111.1258',TO_DATE('07-12-2007','DD-MM-YYYY'),'CL_MATH',6900,108,100); COMMIT;