0

Having the following JSON data in a column of text type in a Redshift database

genres
["drama","action","comedy"]
["drama","comedy","thriller"]
["drama","romance"]

I'd like to combine them to text using a custom delimiter to something like

genres
drama|action|comedy
drama|comedy|thriller
drama|romance

I see that there are some solutions available in Postgres, but my question is how to accomplish the same in Amazon Redshift.

1 Answer 1

1
+50

Since genres is json-compliant, you can use the super type, PartiQL, and some list aggregation to accomplish this.

Let's make some tables.

create temporary table _so (
  genres text
);

insert into _so values
  ('["drama","action","comedy"]'),
  ('["drama","comedy","thriller"]'),
  ('["drama","romance"]');

The basic pattern here is to convert your column into the super type (supered), use PartiQL to pivot the values into rows (super_pivot), then aggregate those string values after converting them back into strings. (lag).

with supered as (
  select 
    json_parse(genres) genres
  from
    _so
), super_pivot as (
  select
    sup.genres,
    item
  from
    supered as sup,
    sup.genres as item
), lag as (
  select
    genres,
    listagg(cast(item as varchar), '|') delimited
  from
    super_pivot
  group by
    genres
)
select * from lag;

Note that this presumes genres is unique. If it is not, perhaps make a row_number or something to aggregate on later so you end up with the same number of rows as your starting point.

genres delimited
["drama","comedy","thriller"] drama|comedy|thriller
["drama","action","comedy"] drama|action|comedy
["drama","romance"] drama|romance
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.