oracle 使用 grouping 函数可以,Oracle GROUPING函数的使用

GROUPING函数可以接受一列,返回0或者1。若是列值为空,那么GROUPING()返回1;若是列值非空,那么返回0。GROUPING只能在使用ROLLUP或CUBE的查询中使用。当需要在返回空值的地方显示某个值时,GROUPING()就异常有用。

1、在ROLLUP中对单列使用GROUPING()

SQL> select division_id,sum(salary)

2  from employees2

3  group by rollup(division_id)

4  order by division_id;

DIV SUM(SALARY)

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

BUS     1610000

OPE     1320000

SAL     4936000

SUP     1015000

8881000

加上GROUPING来看看

SQL> select grouping(division_id),division_id,sum(salary)

2  from employees2

3  group by rollup(division_id)

4  order by division_id;

GROUPING(DIVISION_ID) DIV SUM(SALARY)

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

0 BUS     1610000

0 OPE     1320000

0 SAL     4936000

0 SUP     1015000

1         8881000

可以看到,为空的地方返回1,非空的地方返回0。

2、使用CASE转换GROUPING()的返回值

可能你会以为前面的0和1太死板了,代表不了任何意义,说白了就是不够人性化,呵呵。这个时刻我们可以使用CASE来转换为一些有意义的值。

SQL> select

2  case grouping(division_id)

3  when 1 then 'all divisions'

4  else division_id

5  end as div,

6  sum(salary)

7  from employees2

8  group by rollup(division_id)

9  order by division_id;

DIV           SUM(SALARY)

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

BUS               1610000

OPE               1320000

SAL               4936000

SUP               1015000

all divisions     8881000

3、使用CASE和GROUPING()转换多个列的值

SQL> select

2  case grouping(division_id)

3  when 1 then 'all divisions'

4  else division_id

5  end as div,

6  case grouping(job_id)

7  when 1 then 'all jobs'

8  else job_id

9  end as job,

10  sum(salary)

11  from employees2

12  group by rollup(division_id,job_id)

13  order by division_id,job_id;

DIV           JOB      SUM(SALARY)

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

BUS           MGR           530000

BUS           PRE           800000

BUS           WOR           280000

BUS           all jobs     1610000

OPE           ENG           245000

OPE           MGR           805000

OPE           WOR           270000

OPE           all jobs     1320000

SAL           MGR          4446000

SAL           WOR           490000

SAL           all jobs     4936000

DIV           JOB      SUM(SALARY)

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

SUP           MGR           465000

SUP           TEC           115000

SUP           WOR           435000

SUP           all jobs     1015000

all divisions all jobs     8881000

16 rows selected.

4、CUBE与GROUPING()连系使用

SQL> select

2  case grouping(division_id)

3  when 1 then 'all divisions'

4  else division_id

5  end as div,

6  case grouping(job_id)

7  when 1 then 'all jobs'

8  else job_id

9  end as job,

10  sum(salary)

11  from employees2

12  group by cube(division_id,job_id)

13  order by division_id,job_id;

DIV           JOB      SUM(SALARY)

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

BUS           MGR           530000

BUS           PRE           800000

BUS           WOR           280000

BUS           all jobs     1610000

OPE           ENG           245000

OPE           MGR           805000

OPE           WOR           270000

OPE           all jobs     1320000

SAL           MGR          4446000

SAL           WOR           490000

SAL           all jobs     4936000

DIV           JOB      SUM(SALARY)

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

SUP           MGR           465000

SUP           TEC           115000

SUP           WOR           435000

SUP           all jobs     1015000

all divisions ENG           245000

all divisions MGR          6246000

all divisions PRE           800000

all divisions TEC           115000

all divisions WOR          1475000

all divisions all jobs     8881000

21 rows selected.

5、使用GROUPING SETS子句

使用GROUPING SETS子句可以只返回小计纪录。

SQL> select division_id,job_id,sum(salary)

2  from employees2

3  group by grouping sets(division_id,job_id)

4  order by division_id,job_id;

DIV JOB SUM(SALARY)

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

BUS         1610000

OPE         1320000

SAL         4936000

SUP         1015000

ENG      245000

MGR     6246000

PRE      800000

TEC      115000

WOR     1475000

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值