Oracle - string combinatorial permutation

I think I have a difficult requirement.

This is a combinatorial permutation using Oracle 10.2, I can solve it with Cartesian joins, but I think it needs some improvements to make it simpler and more flexible.

Main behavior.

input line : 'two two'

Output : 'one' 'two' 'one two' 'two one'

For my solution, I limited the number of lines to 5 (note that the output is the number next to the factorial)

SQL:

with My_Input_String as (select 1 as str_id, 'alpha beta omega gama' as str from dual ) --------logic------- , String_Parse as ( SELECT REGEXP_SUBSTR(str, '[^ ]+', 1, ROWNUM) str FROM My_Input_String where rownum < 6 -- string limitation -- CONNECT BY level <= LENGTH(REGEXP_REPLACE(str, '([^ ])+|.', '\1') ) ) --------CRAP select need refactoring------- select str from String_Parse union select REGEXP_REPLACE(trim(s1.str||' '||s2.str||' '||s3.str||' '||s4.str||' '||s5.str), '( ){2,}', ' ') as str from (select str from String_Parse union select ' ' from dual) s1, (select str from String_Parse union select ' ' from dual) s2, (select str from String_Parse union select ' ' from dual) s3, (select str from String_Parse union select ' ' from dual) s4, (select str from String_Parse union select ' ' from dual) s5 where -- s1.str <> s2.str and s1.str <> s3.str and s1.str <> s4.str and s1.str <> s5.str -- and s2.str <> s3.str and s2.str <> s4.str and s2.str <> s5.str -- and s3.str <> s4.str and s3.str <> s5.str -- and s4.str <> s5.str 
+6
source share
1 answer

Edit: it turned out to be general. Really simple at the end (but it took me a while to get there)

 WITH words AS ( SELECT REGEXP_SUBSTR( '&txt', '\S+', 1, LEVEL ) AS word , LEVEL AS num FROM DUAL CONNECT BY LEVEL <= LENGTH( REGEXP_REPLACE( '&txt', '\S+\s*', 'X' ) ) ) SELECT SYS_CONNECT_BY_PATH( W.word, ' ' ) FROM words W CONNECT BY NOCYCLE PRIOR W.num != W.num 

Edit2: Removed redundant maxnum content. Abandoned previous attempts

+8
source

Source: https://habr.com/ru/post/894135/


All Articles