Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Oracle SQL:classifique a linha com string delimitada


Você pode usar REGEXP_SUBSTR para extrair os números individuais de cada linha e, em seguida, usar LISTAGG para juntá-los na ordem desejada - algo como o abaixo:
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