oracle group by rollup小计,CUBE,GROUPING SETS,wm_concat(group by的项目用逗号相连)

group by rollup用来生成小计和总计,经常会合并GROUPING函数使用。
GROUPING:当使用聚集函数(Cube,Rollup等等)的时候,使用grouping函数来区分结果集中的NULL是由于聚集函数产生的还是本身的。如果是由聚集函数产生的,则返回值为1,否则返回0。

wm_concat(group by的项目用逗号相连)

select grouping(t.type_code),grouping(t.year_month),
t.type_code, t.year_month, count(*),
--排行
rank() over (partition by t.type_code order by count(*) desc)
from ar_sell t
group by rollup(t.type_code, t.year_month)
order by t.type_code, t.year_month


wm_concat操作后的字段类型默认是clob的,在python中处理不是很方便(不能用fetchmany)
使用to_char(wm_concat(brand_name))转成varchar后就可以!

rollup(a,b,c) 包括N+1种组合,分别按(a,b,c )聚合,按(a,b)聚合,按(a)聚合,最后()聚合即全表聚合。
cube(a,b,c) 包括2^n种组合,分别按(a,b,c),(a,b),(a,c),(a),(b,c),(b),(c),()共8种情况聚合。grouping和grouping_id()可以美化效果。(显示合计,小计)

参考[url=http://xsb.itpub.net/post/419/44634]Oracle 分析函数的使用(zt) [/url]

select decode(grouping(m.province), '0', m.province, '1', '汇总') as province,sum(m.num)
from tbl m group by rollup(m.province)


select t.type_code,
2010,
t.province,
t.city,
t.country,
t.shop_name,
sum(t.sell_money),
sum(t.sell_money) / 2,
to_char(wm_concat(t.brand_name || '$AR$' || t.sell_money))
from ar_sell t
where t.year_month >= 201001
and t.year_month <= 201012
group by t.type_code, t.province, t.city, t.country, t.shop_name
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值