somanyquestions
somanyquestions

Reputation: 41

Lateral Unnest -SQL

Lateral Unnest -ing gives me a bit of a problem.

I have a table That looks like:

Name  | Info
------|------
  A   | Info1, Info2, Info3
  B   | Info1, Info 2, Info3

I want it to look like is:

Name  | Info
------|------
 A    | Info1 
 A    | Info2 
 A    | Info3 
 B    | Info1 
 B    | Info2 
 B    | Info3 

There's a few more columns in there. I want to all keep them. Any Ideas?

Upvotes: 1

Views: 4940

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269463

If you want to use unnest():

select t.name, trim(i) as info
from t cross join lateral
     unnest(string_to_array(t.info, ',')) i;

You don't need to use unnest(), because you can split directly to an array:

select t.name, trim(i) as info
from t cross join lateral
     regexp_split_to_table(t.info, ',') i;

Upvotes: 4

Related Questions