wm_concat函数的排序问题

wm_concat在行转列的时候非常有用,但在行转列的过程中的排序问题常常难以控制。

可见下面例子:
准备测试表:
drop table t;
create table t (n number,m number);
insert into t values(1,1);
insert into t values(5,3);
insert into t values(3,3);
insert into t values(6,5);
insert into t values(7,2);
insert into t values(2,2);
insert into t values(0,1);
insert into t values(11,1);
insert into t values(15,3);
insert into t values(13,3);
insert into t values(16,5);
insert into t values(17,2);
insert into t values(12,2);
insert into t values(10,1);
commit;

SQL> select * from t order by 2,1;
 
         N          M
———- ———-
         0          1
         1          1
        10          1
        11          1
         2          2
         7          2
        12          2
        17          2
         3          3
         5          3
        13          3
        15          3
         6          5
        16          5
       
测试wm_concat后的顺序:
测试1:
SQL> select m,wm_concat(n) from t group by m;
 
         M WM_CONCAT(N)
———- ——————————————————————————–
         1 11,0,1,10
         2 17,2,7,12
         3 15,3,5,13
         5 16,6
        
可见wm_concat后的顺序并没有按大->小,或小->大的顺序。

测试2:
–参考网上一些解决思路
SQL> select m,wm_concat(n)
  2    from (select n,m from t order by m,n )
  3   group by m;
 
         M WM_CONCAT(N)
———- ——————————————————————————–
         1 0,11,10,1
         2 2,17,12,7
         3 3,15,13,5
         5 6,16

可见顺序问题还是没有解决

最终解决思路:
SQL> select m, max(r)
  2    from (select m, wm_concat(n) over (partition by m order by n) r from t)
  3   group by m ;
 
         M MAX(R)
———- ——————————————————————————–
         1 0,1,10,11
         2 2,7,12,17
         3 3,5,13,15
         5 6,16

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

转载于:http://blog.itpub.net/10009036/viewspace-1061450/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值