Oracle分析函数-1

本文通过实战案例介绍了SQL中GROUP BY语句的两种用法:GROUPING SETS和ROLLUP,详细展示了如何使用这两种方法进行数据分组汇总,并提供了具体的数据结果以便读者更好地理解和掌握。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习笔记

[@more@]

Ø e创建基础数据

create table hyf_t as
select object_name ename,mod(object_id,
50) deptno, created fired_date, object_id sal
from all_objects where rownum<=
200

create index hyf_t on hyf_t(deptno,fired_date,ename)

Ø GROUP函数

AGROUPING SETS

select deptno, ename, sum(sal) sal
from hyf_t

where deptno <2
group by grouping sets((deptno, ename), deptno)
order by deptno

DEPTNO

ENAME

SAL

0

AUDIT$

150

0

DBMS_STANDARD

650

0

FLOAT

600

0

V_$PGA_TARGET_ADVICE_HISTOGRAM

700

0

V_$SESSION_EVENT

1000

0

V_$SPPARAMETER

850

0

11300

1

INTEGER

601

1

I_AUDIT

151

1

V$OPTION

901

1

V$PGA_TARGET_ADVICE_HISTOGRAM

701

1

V$SESSION_EVENT

1001

1

V$SPPARAMETER

851

1

V_$SESSION_OBJECT_CACHE

1051

1

11320

/*--------理解grouping sets

select a, b, c, sum( d ) from t

group by grouping sets ( a, b, c )

等效于

select * from (

select a, null, null, sum( d ) from t group by a

union all

select null, b, null, sum( d ) from t group by b

union all

select null, null, c, sum( d ) from t group by c

)

*/

--BROLLUP

select deptno, ename, sum(sal) sal
from hyf_t
where deptno <
2
group by rollup(deptno, ename)
order by deptno

deptno

ename

sal

0

AUDIT$

150

0

DBMS_STANDARD

650

… …

0

V_$SESSION_EVENT

1000

0

V_$SPPARAMETER

850

0

11300

1

INTEGER

601

… …

1

I_AUDIT

151

1

V_$SESSION_OBJECT_CACHE

1051

1

11320

22620

/*--------理解rollup

select a, b, c, sum( d )

from t

group by rollup(a, b, c);

等效于

select * from (

select a, b, c, sum( d ) from t group by a, b, c

union all

select a, b, null, sum( d ) from t group by a, b

union all

select a, null, null, sum( d ) from t group by a

union all

select null, null, null, sum( d ) from t

)

*/

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/703656/viewspace-812317/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/703656/viewspace-812317/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值