ROLLUP()函數(shù)是對(duì)于GROUP BY 分組統(tǒng)計(jì)的功能擴(kuò)展,可以實(shí)現(xiàn)分組統(tǒng)計(jì)求和的效果。
網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序定制開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了君山免費(fèi)建站歡迎大家使用!
下面我們開(kāi)始準(zhǔn)備我們的試驗(yàn)的環(huán)境
--創(chuàng)建新表employee_salary,存儲(chǔ)數(shù)據(jù)來(lái)自用戶hr.employees
SQL>CREATE TABLE employee_salaryASSELECT E.FIRST_NAME,E.JOB_ID,E.MANAGER_ID,E.SALARYFROM HR.EMPLOYEES EWHERE E.JOB_ID='IT_PROG';
--查看新創(chuàng)建的表
SQL>SELECT *FROM employee_salary;
--顯示效果如下
FIRST_NAME JOB_ID MANAGER_ID SALARY
-------------------- ---------- --------------------------------
Alexander IT_PROG 102 9000.00
Bruce IT_PROG 103 6000.00
David IT_PROG 103 4800.00
Valli IT_PROG 103 4800.00
Diana IT_PROG 103 4200.00
--首先按照J(rèn)OB_ID進(jìn)行分組,查看salary和
SQL>SELECT sa.job_id,SUM(sa.salary) FROM employee_salary saGROUP BY sa.job_id;
--顯示效果如下
JOB_ID SUM(SA.SALARY)
---------- -----------------------
IT_PROG 28800
--按照MANAGER_ID進(jìn)行分組,查看salary和
SELECT sa.manager_id,SUM(sa.salary)FROM employee_salary saGROUP BY sa.manager_id;
--顯示效果如下
MANAGER_ID SUM(SA.SALARY)
---------- ----------------------------
102 9000
103 19800
--我們使用ROLLUP函數(shù)看看是什么效果
SELECT sa.job_id,sa.manager_id,SUM(sa.salary)FROM employee_salary saGROUP BY ROLLUP(sa.job_id,sa.manager_id);
--顯示效果如下
JOB_ID MANAGER_ID SUM(SA.SALARY)
---------- ---------- --------------------------------
IT_PROG 102 9000
IT_PROG 103 19800
IT_PROG 28800
28800
說(shuō)明:ROLLUP解析過(guò)程,以ROLLUP(a,b)為例
ROLLUP(a,b)== GROUP(a,b) UNION ALL GROUP(a) UNIONALL GROUP()
即:解析順序是從右至左,顯示按照a,b分組,接下來(lái)是按照a分組,最后是對(duì)全表分組;
上面ROLLUP (sa.job_id,sa.manager_id)等同下面UNION ALL的集合操作
SELECT sa.job_id,sa.manager_id,SUM(sa.salary)FROM employee_salary sa
GROUP BY sa.job_id,sa.manager_id
UNION ALL
SELECT sa.job_id,NULL,SUM(sa.salary) FROM employee_salary sa
GROUP BY sa.job_id
UNION ALL
SELECT NULL,NULL,SUM(sa.salary) FROM employee_salary saGROUP BY()
ORDER BY1,2;
--顯示效果如下
JOB_ID MANAGER_ID SUM(SA.SALARY)
---------- ---------- --------------------------------
IT_PROG 102 9000
IT_PROG 103 19800
IT_PROG 28800
28800
說(shuō)明:雖然最后展示的效果是相同的,但是ROLLUP()函數(shù)的執(zhí)行效率要比UNION ALL的效率要高、要快。
ROLLUP(A,B,C)是在執(zhí)行組合操作,無(wú)順序,組合公式是(n+1),當(dāng)n=3時(shí),組合結(jié)果就是有4個(gè)。
ROLLUP()中的參數(shù)位置不同,得出的結(jié)果可能不一樣!
在理解ROLLUP的基礎(chǔ)上再來(lái)理解CUBE()就比較容易,ROLLUP()在執(zhí)行組合操作,CUBE()就是在執(zhí)行排序動(dòng)作,從左至右,排序公式是2N次方。
CUBE(A,B,C)==GROUP BY (A,B,C) UNION ALL GROUP BY (A,B) UNION ALL GROUP BY (A,C) UNION ALL GROUPBY (A) UNION ALL GROUP BY (B) UNION ALL GROUP BY (C) UNION ALL GROUP BY ()
例如:執(zhí)行下列語(yǔ)句
SQL> SELECT sa.job_id,sa.manager_id,SUM(sa.salary)FROM employee_salary saGROUP BY CUBE(sa.job_id,sa.manager_id);
--顯示效果如下
JOB_ID MANAGER_ID SUM(SA.SALARY)
---------- ---------- -----------------------------
28800
102 9000
103 19800
IT_PROG 28800
IT_PROG 102 9000
IT_PROG 103 19800
6 rows selected
上面的語(yǔ)句等同執(zhí)行下列語(yǔ)句
SQL>SELECT sa.job_id,sa.manager_id,SUM(sa.salary)FROM employee_salary saGROUP BY (sa.job_id,sa.manager_id)
UNION ALL
SELECT sa.job_id,NULL,SUM(sa.salary)FROM employee_salary sa
GROUP BY (sa.job_id)
UNION ALL
SELECT NULL,sa.manager_id,SUM(sa.salary)FROM employee_salary sa
GROUP BY (sa.manager_id)
UNION ALL
SELECT NULL,NULL,SUM(sa.salary)FROM employee_salary sa
GROUPBY ();
關(guān)于GROUPING
GROUPING(A)用于判斷對(duì)于分組后的列是否是空值NULL,返回值有0和1兩個(gè)值,1表示,該列為空——NULL,這個(gè)NULL值是因?yàn)榉纸M時(shí)產(chǎn)生,否則則為0;