Skip to content

SQL Collect duplicates to one place? PostgreSQL

Sorry I’m new here and I’m also new with SQL and can’t really explain my problem in the title…

So I have a TV show database, and there I have a Genre column, but for a TV show there are multiple Genres stored, so when I’m selecting all my TV Shows how can I combine them?

It needs to look like this:

https://i.stack.imgur.com/3EhBj.png

So I have to combine the string together, here is my code so far what I wrote:

I also have some other stuff here, that’s my exerciese tasks! Thanks!

Also here is the relationship model:

https://i.stack.imgur.com/M89ho.png

Advertisement

Answer

Basically you need string aggregation – in Postgres, you can use string_agg() for this.

For efficiency, I would recommend moving the aggregation to a correlated subquery or a lateral join rather than aggregating in the outer query, so:

User contributions licensed under: CC BY-SA
5 People found this is helpful