Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

sql - Unable to find alternative for connect by level in postgresql

select 
trim(regexp_substr(t.column_A, '[^,]+', 1, levels.column_value)) as column_A 

from (SELECT * FROM table_t t ) t, 

table(cast(multiset(select level from dual 
connect by level <= length (regexp_replace(t.column_A, '[^,]+')) + 1) as sys.OdciNumberList)) levels

Not able to find alternate for connect by level in postgresql. Also the dual don't exist in postgresql.

It would be great help if someone help me to identify alternative for the above posted query.

question from:https://stackoverflow.com/questions/65939402/unable-to-find-alternative-for-connect-by-level-in-postgresql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You did not define your table_t so I will assume that column_a is a comma separated string as that appears to be what the Oracle is looking for. If that is correct then perhaps the simplest approach is to transform the string to an array, then unroll the array. Fortunately, Postgres provides single function for each action string_to_array and unnest respectively. (See documentation: Array Functions and Operators).

with table_t( column_a) as
     ( values ( 'abc,defgh,ijklm,nopqr,stuvw,zyz0123,456789'))
select unnest (string_to_array(column_a,','))
  from table_t; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...