简体   繁体   中英

How to split a row into multiple rows with a single query?

This:

column_A|column_B|column_C
a|a,b,c|b
b|a|b
c|a|c

should end up like this:

column_A|column_B|column_C
a|a|b
a|b|b
a|c|b
b|a|b
c|a|c

column_B has CSV like data. columns are seperated by an arbitrary character in this case a comma.

You should be able to accomplish this with the combination of string_to_array and unnest :

SELECT column_a, unnest(string_to_array(column_b,',')), column_c 
FROM   tablename;

SQLFiddle

That said, you did not give enough information about the datatype and an arbitrary character is never arbitrary if it is acting as a delimiter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM