`
desert3
  • 浏览: 2140985 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
社区版块
存档分类
最新评论

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()可以美化效果。(显示合计,小计)

参考Oracle 分析函数的使用(zt)

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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics