The question of how to split a field (e.g. a CSV string) into multiple rows has already been answered: Split values over multiple rows.
However, this question refers to MSSQL, and the answers use various features for which there are no RedShift equivalents.
For the sake of completeness, here's an example of what I'd like to do:
Current data:
| Key | Data |
+-----+----------+
| 1 | 18,20,22 |
| 2 | 17,19 |
Required data:
| Key | Data |
+-----+----------+
| 1 | 18 |
| 1 | 20 |
| 1 | 22 |
| 2 | 17 |
| 2 | 19 |
Now, I can suggest a walkaround for the case of small, bounded number of elements in the CSV field: use split_part and union over all possible array locations, like so:
SELECT Key, split_part(Data, ',', 1)
FROM mytable
WHERE split_part(Data, ',', 1) != ""
UNION
SELECT Key, split_part(Data, ',', 2)
FROM mytable
WHERE split_part(Data, ',', 2) != ""
-- etc. etc.
However, this is obviously very inefficient, and would not work for longer lists.
Any better ideas on how to do this?
EDIT:
There's also a somewhat similar question regarding multiplying rows: splitting rows in Redshift. However I don't see how this approach can be applied here.
EDIT 2:
A possible duplicate: Redshift. Convert comma delimited values into rows. But nothing new - the answer by @Masashi Miyazaki is similar to my suggestion above, and suffers from the same issues.