Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a comma-separated field in Postgresql and doing a UNION ALL on all the resulting tables

Tags:

I have a table that contains a field of comma separated strings:

ID | fruits ----------- 1  | cherry,apple,grape  2  | apple,orange,peach  

I want to create a normalized version of the table, like this:

ID | fruits ----------- 1  | cherry  1  | apple  1  | grape 2  | apple  2  | orange  2  | peach  

The postgresql 8.4 documentation describes a regexp_split_to_table function that can turn a single table:

SELECT foo  FROM regexp_split_to_table('the quick brown fox jumped over the lazy dog',E'\\s+') AS foo; 

which gives you this:

  foo    --------   the       quick     brown     fox       jumped    over      the       lazy      dog      (9 rows) 

But that is just for a single field. What I want to do is some kind UNION applied to all the tables generated by splitting each field. Thank you.

like image 796
John Horton Avatar asked Feb 10 '12 18:02

John Horton


People also ask

How do I split a column in PostgreSQL?

We can use any of the string to split it; we can also use a column name as a substring to split the data from the column. Delimiter argument is used to split the string into sub-parts by using a split_part function in PostgreSQL. We can split the string into a number of parts using delimiter.

How do I split text in PostgreSQL?

The PostgreSQL SPLIT_PART() function is used to split a string from a specific delimiter and these queries return the nth substring. Let's analyze the above syntax: The string argument is the string to be split. The delimiter is a string used as the delimiter for splitting.

What is Regexp_split_to_table?

regexp_split_to_table() is a system function for splitting a string into a table using a POSIX regular expression as the delimiter.


1 Answers

This should give you the output you're looking for:

SELECT      yourTable.ID,      regexp_split_to_table(yourTable.fruits, E',') AS split_fruits FROM yourTable 

EDIT: Fixed the regex.

like image 73
Michael Fredrickson Avatar answered Sep 29 '22 06:09

Michael Fredrickson