Skip to content
Advertisement

PostgreSQL: Make data appear in different rows instead of same row

I have this query:

And the result gives me a table with 4 columns and 1 row:

e.g. Blue | Purple | Yellow | Mixed

But I want the results to appear in 4 rows and 1 column like this:

Blue

  • Blue
  • Purple
  • Yellow
  • Mixed

Does anyone have any idea what I could do in this case? I’ve been working on this forever.

Advertisement

Answer

This is a bit of a pain, because of the mixed value — a simple aggregation is not sufficient. So, use a lateral join to unpivot:

Actually, in Postgres, you could also write this as:

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