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