Ваш комментарий к @PrzemyslawKruglej answer
Главная проблема связана с внутренним запросом с connect by
, его генерирует удивительное количество рядов
Количество сгенерированных строк может быть уменьшено с помощью следующего подхода:
/* 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;
Результат: Для три строки, в которых самый длинный один состоит из трех слов, мы будем генерировать 9 строк:
GENERATED_FOR_3_ROWS
--------------------
9
Окончательный запрос:
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;
Результат:
RES CNT
----- ----------
a 2
b 2
c 3
d 2
SQLFIddle Demo
Узнать больше о regexp_count() (11g и выше) и regexp_substr() функции регулярного выражения.
Примечание: Функции регулярного выражения относительно дороги для вычисления, и когда дело доходит до обработки очень большого количества данных, возможно, стоит рассмотреть возможность переключения на простой PL/SQL. Here is an example.
[Вы FINDER ответ здесь] (https://blogs.oracle.com/aramamoo/entry/how_to_split_comma_separated_string_and_pass_to_in_clause_of_select_statement) –
@PrzemyslawKruglej, я видел этот пример. Он работает только в том случае, если у меня есть одна строка, но у меня много строк. –