ORACLE rollup函數


rollup函數應用場景:

  主要使用在 分組中,將每個分組求匯總值(就是小計),最后再講所有值(除去小計)求和(就是合計)

當然,使用union 也可以達到同樣的效果。先將需要查詢的分組查出來,再union上和

然后再union上所有分組的和。

當然,分組比較多的時候,就吐血了。

 

實例:

select nvl(type,'合計') type, case when type is null then null else nvl(class,'小計') end class,sum(grade) grade 
from table_t 
where col1 = 3 and col2 =1
group by rollup(type,class)

結果集:

  

如果不要合計可以這樣用:

select * from(select nvl(type,'合計') type, case when type is null then null else nvl(class,'小計') end class,sum(grade) grade 
from table_t 
where col1 = 3 and col2 =1
group by rollup(type,t.class)) where type is not null

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM