Skip to content
Advertisement

How do I pivot values from rows into column names in Postgres?

I have a query in Postgres. It’s working well and returns exactly the rows I’m looking for.

Here’s the query:

This is what it returns:

id status timestamp
1 started 2022-01-15 05:12:36
1 paused 2022-04-14 09:12:50
1 killed 2022-04-27 13:12:48

How can I pivot this table to return this exactly:

id started paused killed
1 2022-01-15 05:12:36 2022-04-14 09:12:50 2022-04-27 13:12:48

Advertisement

Answer

You could use the extension tablefunc, but it looks like a simple aggregate using a filter already works:

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