Redshift. Convert comma delimited values into rows

后端 未结 8 1187
北恋
北恋 2020-12-01 06:25

I am wondering how to convert comma-delimited values into rows in Redshift. I am afraid that my own solution isn\'t optimal. Please advise. I have table with one of the colu

8条回答
  •  醉话见心
    2020-12-01 07:03

    Here's my equally-terrible answer.

    I have a users table, and then an events table with a column that is just a comma-delimited string of users at said event. eg

    event_id | user_ids
    1        | 5,18,25,99,105
    

    In this case, I used the LIKE and wildcard functions to build a new table that represents each event-user edge.

    SELECT e.event_id, u.id as user_id
    FROM events e
    LEFT JOIN users u ON e.user_ids like '%' || u.id || '%'
    

    It's not pretty, but I throw it in a WITH clause so that I don't have to run it more than once per query. I'll likely just build an ETL to create that table every night anyway.

    Also, this only works if you have a second table that does have one row per unique possibility. If not, you could do LISTAGG to get a single cell with all your values, export that to a CSV and reupload that as a table to help.

    Like I said: a terrible, no-good solution.

提交回复
热议问题