Have a comma separated string in my table field (converted to a comma separated string using STRING_AGG with the user_id). Wish to separate the string into separate columns in the output. Is there some way to do this using PostgreSQL?
I am unable to use CROSSTAB() on this, as I am running the query in Amazon QuickSight, which does not allow the function: PostgreSQL Crosstab Query
Have tried using the SPLIT_PART(), but as I am not sure how many values there will be in each row, and also want that each column should have the same word in it, or be blank, if that word is not there in the row. The original SQL to convert the individual rows to a comma separated value is:
    SELECT 
        ss.ss_uuid,
        STRING_AGG(mn.name, ', ') AS markers
    FROM serial_state AS ss
    JOIN marker_names AS mn
    ON mn.s_uuid = ss.mn_s_uuid
    GROUP BY ss.ss_uuid
Output of Query 1:
| ss_uuid | markers       |
| abcdefg | my,words,here |
| bcdefgh | words,my      |
| fghijkl | my,here       |
| opqrstu | here.my       |
| xyzabcd | here,my,words |
| pqrstuv | words         |
| restgte | my,you,here,i |
I have used the following to get the same into a column:
SELECT
    split_part(markers, ',', 1) AS "Marker Name1",
    split_part(markers, ',', 1) AS "Marker Name2",
    split_part(markers, ',', 1) AS "Marker Name3"
FROM ( 
    SELECT 
        ss.ss_uuid,
        STRING_AGG(mn.name, ', ' ORDER BY mn.name) AS markers
    FROM serial_state AS ss
    JOIN marker_names AS mn
    ON mn.s_uuid = ss.mn_s_uuid
    GROUP BY ss.ss_uuid
) t
Output for query 2:
| Marker Name 1 | Marker Name 2 | Marker Name3 |
| my            | words         | here         |
| words         | my            |              |
| my            | here          |              |
| here          | here          |              |
| here          | my            | words        |
| words         |               |              |
| my            | you           | here         |  -- i is missing from this row
But this query has the 2 issues as highlighted above:
- Do not know how many words there are in the string, so if there are more than 3, then I miss out
- As the initial 'comma separated' string will be not always be composed of the same words, the "Marker Name1" can contain names which are different.
Is there any way that I can resolve this, with a loop (IF/WHERE/WHILE clauses?) or something else?
Update 2:
Output I need:
| Marker Name 1 | Marker Name 2 | Marker Name3 | Marker Name4 | Marker Name5 |
| my            | words         | here         |
| my            | words         |              |
| my            |               | here         |
|               |               | here         |
|               | words         | here         |
|               | words         |              |
| my            |               | here         | you          | i            |  
Update: Was thinking something along these lines:
lenOfString = STRING_TO_ARRAY(flags);
FOR i IN lenOfString LOOP
    SELECT
        split_part(markers, ',', 1) AS "Flag Name" 
    FROM ( 
        SELECT 
            ss.ss_uuid,
            STRING_AGG(mn.name, ', ') AS flags
        FROM serial_state AS ss
        JOIN marker_names AS mn
        ON mn.s_uuid = ss.mn_s_uuid
        GROUP BY ss.ss_uuid, mn.name
    ) t
END LOOP
But that gives an error for the lenOfString
Sources checked for answers on StackOverflow:
SQL Server 2014 : Convert two comma separated string into two columns
