group by rollup 和grouping的使用实例
2013-06-18 10:49
274 查看
GROUPING函数可以接受一列,返回0或者1。如果列值为空,那么GROUPING()返回1;如果列值非空,那么返回0。GROUPING只能在使用ROLLUP或CUBE的查询中使用。当需要在返回空值的地方显示某个值时,GROUPING()就非常有用。
用下面的sql执行一下,就可以知道group by rollup和grouping的用法
with a as (select 'dev1' col1,'file1' col2,'aa' col3,'bb' col4,1 col5 from dual
union all select 'dev1' col1,'file2' col2,'cc' col3,'dd' col4,3 col5 from dual
union all select 'dev2' col1,'file3' col2,'ee' col3,'ff' col4,7 col5 from dual
union all select 'dev2' col1,'file4' col2,'gg' col3,'hh' col4,4 col5 from dual
)
select case when grouping(col1)=1 and grouping(col2)=1 then '总计' when grouping(col2)=1 then '合计' else col1 end as col_1,
case grouping(col2) when 1 then '- -' else col2 end as col_2,
case grouping(col3) when 1 then '- -' else col3 end as col_3,
case grouping(col4) when 1 then '- -' else col4 end as col_4,
sum(col5)
from a
group by rollup(col1,(col2,col3,col4));
用下面的sql执行一下,就可以知道group by rollup和grouping的用法
with a as (select 'dev1' col1,'file1' col2,'aa' col3,'bb' col4,1 col5 from dual
union all select 'dev1' col1,'file2' col2,'cc' col3,'dd' col4,3 col5 from dual
union all select 'dev2' col1,'file3' col2,'ee' col3,'ff' col4,7 col5 from dual
union all select 'dev2' col1,'file4' col2,'gg' col3,'hh' col4,4 col5 from dual
)
select case when grouping(col1)=1 and grouping(col2)=1 then '总计' when grouping(col2)=1 then '合计' else col1 end as col_1,
case grouping(col2) when 1 then '- -' else col2 end as col_2,
case grouping(col3) when 1 then '- -' else col3 end as col_3,
case grouping(col4) when 1 then '- -' else col4 end as col_4,
sum(col5)
from a
group by rollup(col1,(col2,col3,col4));
相关文章推荐
- oracle decode,grouping,group by rollup的用法实例
- group by后使用rollup子句总结
- oracle 的 group by 和 rollup,cube 的使用。
- Oracle group by 基本及的拓展 ROLLUP, CUBE, GROUPING 功能and GROUPING 集合
- Group By 多个分组集小结 --GROUPING SETS,GROUP BY CUBE,GROUP BY ROLLUP,GROUPING(),GROUPING_ID()
- oracle 中 rollup、cube、grouping 使用详解
- group by的rollup、cube、grouping、grouping sets 的用法浅析
- oralce分析函数---group by || rollup || cude || grouping || grouping sets
- SQLServer中汇总功能的使用GROUPING,ROLLUP和CUBE
- SQL编程实例:Access数据库,两张表的统计,count、sum聚合函数的使用,iif的使用,group by的使用
- 在查询结果后的最后一行加上合计,不用 union all,使用GROUP BY ROLLUP grouping
- Oracle分组ROLLUP、GROUP BY、GROUPING、GROUPING SETS区别和作用
- group by(rollup ,cube ,grouping sets) and grouping
- [每日一题] OCP1z0-047 :2013-08-20 GROUP BY扩展――GROUPING、ROLLUP.....................88
- Group by,Grouping,Rollup
- oracle 中 rollup、cube、grouping 使用详解 -- 使用oracle 样例表演示
- [每日一题] OCP1z0-047 :2013-08-20 GROUP BY扩展――GROUPING、ROLLUP.....................88
- group by后加rollup子句的用法以及与cube和grouping sets子句的区别
- group by ()函数汇总之group by rollup()、group by cube()、grouping()
- Oracle很实用的汇总报表实现方式!grouping_id、rollup的使用