我的SQL查询(在Oracle 11g中)返回如下列:
31132,11100,44232
25464,89453,15686
21561,68547,51612
我想订购这些细胞,我的意思是:
11100,31132,44232
15686,25464,89453
21561,51612,68547
我该怎么做?
提前致谢!
答案 0 :(得分:0)
您可以使用REGEXP_SUBSTR从每行中提取单个数字,然后使用LISTAGG以所需顺序重新加入它们 - 如下所示:
WITH
T (N) AS --TEST DATA
(SELECT '31132,11100,44232' FROM DUAL
UNION
SELECT '25464,89453,15686' FROM DUAL
UNION
SELECT '21561,68547,51612' FROM DUAL),
T1 (N1) AS --USE THIS BELOW TO SEPARATE OUT FIRST/SECOND/THIRD VALUE FROM EACH ROW
(SELECT 1 FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL),
SEPARATED (RN,N) AS
(SELECT RN,REGEXP_SUBSTR(N,'[^,]+',1,T1.N1) N FROM --GET THE VALUES FROM EACH ROW WITH AN ACCOMPANYING ROW NUMBER SO WE CAN REGROUP THEM BELOW
(SELECT ROW_NUMBER() OVER(ORDER BY 1) RN ,N FROM T),T1)
SELECT LISTAGG(N,',') WITHIN GROUP (ORDER BY N) FROM SEPARATED GROUP BY RN; --USE LISTAGG TO REJOIN THE SEPARATED VALUES IN THE DESIRED ORDER