De su comentario a la respuesta de @PrzemyslawKruglej
El principal problema es con la consulta interna con connect by
, genera una cantidad asombrosa de filas
La cantidad de filas generadas se puede reducir con el siguiente enfoque:
/* test table populated with sample data from your question */
SQL> create table t1(str) as(
2 select 'a;b;c' from dual union all
3 select 'b;c;d' from dual union all
4 select 'a;c;d' from dual
5 );
Table created
-- number of rows generated will solely depend on the most longest
-- string.
-- If (say) the longest string contains 3 words (wont count separator `;`)
-- and we have 100 rows in our table, then we will end up with 300 rows
-- for further processing , no more.
with occurrence(ocr) as(
select level
from ( select max(regexp_count(str, '[^;]+')) as mx_t
from t1 ) t
connect by level <= mx_t
)
select count(regexp_substr(t1.str, '[^;]+', 1, o.ocr)) as generated_for_3_rows
from t1
cross join occurrence o;
Resultado:Para tres filas donde la más larga se compone de tres palabras, generaremos 9 filas :
GENERATED_FOR_3_ROWS
--------------------
9
Consulta final:
with occurrence(ocr) as(
select level
from ( select max(regexp_count(str, '[^;]+')) as mx_t
from t1 ) t
connect by level <= mx_t
)
select res
, count(res) as cnt
from (select regexp_substr(t1.str, '[^;]+', 1, o.ocr) as res
from t1
cross join occurrence o)
where res is not null
group by res
order by res;
Resultado:
RES CNT
----- ----------
a 2
b 2
c 3
d 2
Demostración de SQLFIddle
Obtenga más información sobre las funciones de expresión regular regexp_count()(11g y más) y regexp_substr().