Oracle
 sql >> Database >  >> RDS >> Oracle

Distinto dai valori CSV utilizzando REGEXP_REPLACE in Oracle

Puoi usare le raccolte di Oracles. CAST() abbinato a COLLECT() può aggregare valori in una raccolta definita dall'utente e quindi SET() eliminerà i duplicati. Quindi puoi usare LISTAGG() per convertire la raccolta in una stringa.

Impostazione Oracle :

CREATE TYPE intlist IS TABLE OF INT;
/

CREATE TYPE stringlist IS TABLE OF VARCHAR2(4000);
/

CREATE TABLE table_name ( ColA NUMBER(5,0), ColB VARCHAR2(20) );
INSERT INTO table_name
  SELECT 1, 'POW' FROM DUAL UNION ALL
  SELECT 2, 'POW' FROM DUAL UNION ALL
  SELECT 1, 'POWPRO' FROM DUAL UNION ALL
  SELECT 1, 'PRO' FROM DUAL UNION ALL
  SELECT 2, 'PRO' FROM DUAL UNION ALL
  SELECT 1, 'PROUTL' FROM DUAL UNION ALL
  SELECT 1, 'TNEUTL' FROM DUAL UNION ALL
  SELECT 1, 'UTL' FROM DUAL UNION ALL
  SELECT 1, 'UTLTNE' FROM DUAL;

Richiesta :

SELECT ( SELECT LISTAGG( COLUMN_VALUE, ',' )
                  WITHIN GROUP ( ORDER BY COLUMN_VALUE )
         FROM   TABLE( ColA ) ) AS ColA,
       ( SELECT LISTAGG( COLUMN_VALUE, ',' )
                  WITHIN GROUP ( ORDER BY COLUMN_VALUE )
         FROM   TABLE( ColB ) ) AS ColB  
FROM   (
  SELECT SET( CAST( COLLECT( ColA ORDER BY ColA ) AS INTLIST ) ) ColA,
         SET( CAST( COLLECT( ColB ORDER BY ColB ) AS STRINGLIST ) ) ColB
  FROM   table_name
);

Risultato :

ColA ColB
---- ---------------------------------------
1,2  POW,POWPRO,PRO,PROUTL,TNEUTL,UTL,UTLTNE