首页 > 代码库 > db2的rollup和cube函数
db2的rollup和cube函数
rollup的效果就是对group by后面的一个分组列名进行统计。
bankid为第一列
SELECT CASE WHEN grouping (branchid) = 0 THEN branchid ELSE '小计' END as branchid, CASE WHEN grouping (bankid) = 0 THEN bankid ELSE '总计' END as bankid, SUM (balance) FROM ebanksumm WHERE workdate = '20140320' GROUP BY bankid , branchid with rollup
branchid为第一列
SELECT CASE WHEN grouping (branchid) = 0 THEN branchid ELSE '小计' END AS branchid , CASE WHEN grouping (bankid) = 0 THEN bankid ELSE '总计' END AS bankid , SUM (balance) FROM ebanksumm WHERE workdate = '20140321' GROUP BY branchid , bankid WITH rollup
cube函数则是对groupby后面的所有列依次进行统计。和rollup的区别就在于要统计的列。rollup只统计一列,cube统计所有列
SELECT CASE WHEN grouping (branchid) = 0 THEN branchid ELSE '小计' END as branchid, CASE WHEN grouping (bankid) = 0 THEN bankid ELSE '总计' END as bankid, SUM (balance) FROM ebanksumm WHERE workdate = '20140321' GROUP BY bankid , branchid with Cube
db2的rollup和cube函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。