I have a table with a string which contains several delimited values, e.g. a;b;c
.
I need to split this string and use its values in a query. For example I have following table:
str
a;b;c
b;c;d
a;c;d
I need to group by a single value from str
column to get following result:
str count(*)
a 1
b 2
c 3
d 2
Is it possible to implement using single select query? I can not create temporary tables to extract values there and query against that temporary table.
This is ugly, but seems to work. The problem with the
CONNECT BY
splitting is that it returns duplicate rows. I managed to get rid of them, but you'll have to test it:From your comment to @PrzemyslawKruglej answer
The amount of rows generated can be reduced with the following approach:
Result: For three rows where the longest one is made up of three words, we will generate 9 rows:
Final query:
Result:
SQLFIddle Demo
Find out more about regexp_count()(11g and up) and regexp_substr() regular expression functions.
Note: Regular expression functions relatively expensive to compute, and when it comes to processing a very large amount of data, it might be worth considering to switch to a plain PL/SQL. Here is an example.