The CUBE clause return rows containing a subtotal for all combinations of columns along with a total at the end.
CREATE TABLE TEST (NAME VARCHAR2(20),SALARY NUMBER);
INSERT INTO TEST VALUES('SADAT',100);
INSERT INTO TEST VALUES('NAJMUS',200);
INSERT INTO TEST VALUES('SADAT',300);
INSERT INTO TEST VALUES('NAJMUS',400);
SELECT NAME,SUM(SALARY) FROM TEST GROUP BY NAME;
NAME SUM(SALARY)
-------------------- -----------
SADAT 400
NAJMUS 600
SELECT NAME,SUM(SALARY) FROM TEST GROUP BY CUBE(NAME);
NAME SUM(SALARY)
-------------------- -----------
1000
SADAT 400
NAJMUS 600
Again,
ALTER TABLE TEST ADD CITY VARCHAR2(20);
UPDATE TEST SET CITY='BOGRA' WHERE NAME='SADAT';
UPDATE TEST SET CITY='RAJSHAHI' WHERE NAME='NAJMUS';
INSERT INTO TEST VALUES('NAURIN',150,'RAJSHAHI');
SELECT CITY,NAME,SUM(SALARY) FROM TEST GROUP BY CUBE(NAME,CITY);
CITY NAME SUM(SALARY)
-------------------- -------------------- -----------
1150
BOGRA 400
RAJSHAHI 750
SADAT 400
BOGRA SADAT 400
NAJMUS 600
RAJSHAHI NAJMUS 600
NAURIN 150
RAJSHAHI NAURIN 150
No comments:
Post a Comment