博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wm_concat函数的排序问题
阅读量:4619 次
发布时间:2019-06-09

本文共 1266 字,大约阅读时间需要 4 分钟。

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

转载于:https://www.cnblogs.com/wayne-ivan/p/6416489.html

你可能感兴趣的文章
关于 IOS 发布的点点滴滴记录(一)
查看>>
《EMCAScript6入门》读书笔记——14.Promise对象
查看>>
CSS——水平/垂直居中
查看>>
Eclipse连接mysql数据库jdbc下载(图文)
查看>>
Python中Selenium的使用方法
查看>>
三月23日测试Fiddler
查看>>
20171013_数据库新环境后期操作
查看>>
poj 1654 && poj 1675
查看>>
运维派 企业面试题1 监控MySQL主从同步是否异常
查看>>
Docker 版本
查看>>
poj 1753 Flip Game
查看>>
在深信服实习是怎样的体验(研发测试岗)
查看>>
Linux免密码登陆
查看>>
SpringMVC中文件的上传(上传到服务器)和下载问题(二)--------下载
查看>>
Socket & TCP &HTTP
查看>>
osip及eXosip的编译方法
查看>>
Hibernate composite key
查看>>
[CF Round #294 div2] D. A and B and Interesting Substrings 【Map】
查看>>
keepalived+nginx安装配置
查看>>
我的2015---找寻真实的自己
查看>>