设为首页 加入收藏

TOP

Oracle 高级分组group by cube拓展(二)
2015-08-31 20:00:00 来源: 作者: 【 】 浏览:88
Tags:Oracle 高级 分组 group cube 拓展
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? 8? ? ? ? ? 5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? 5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? 1? ? ? ? ? 4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? 2? ? ? ? ? 4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? 3? ? ? ? ? 4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? 4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
19 rows selected.? ?



从上面的输出中,我们会发现cube会将许多的null值,如果cube()里指定的列本身就具有null值,又需要如何区分呢?


解决方法为通过grouping()函数来排除null值,例如grouping(a)来检测A表中a列是否有一行null值是由cube产生的,如果有,则返回1,其他所有情况,则返回0.


接着再结合decode()函数或case表达式,来将cube()产生的null值转化成通俗易懂的字符串。



?实验如下:



HR@ORA11GR2 > select decode(grouping(a),1,'cube_value',a) a , decode(grouping(a2), 1, 'cube_value',a2) a2 from a group by cube(a, a2) order by a2 desc;


?



A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? A2


---------------------------------------- ----------------------------------------


cube_value? ? ? ? ? ? ? ? ? ? ? ? cube_value


4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


8? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


7? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


6? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


3? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


2? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cube_value


cube_value? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5


6? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5


?



A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? A2


---------------------------------------- ----------------------------------------


8? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5


7? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5


4? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5


5? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 5


1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4


cube_value? ? ? ? ? ? ? ? ? ? ? ? 4


3? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4


2? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 4


19 rows selected.


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇通过Shell脚本格式化tnsnames.ora.. 下一篇Oracle AWR常用指标整理

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: