Skip to content
Advertisement

PostgreSQL simply transpose/convert rows to columns

I read some SO posts like sql server 2008 r2 – Transpose rows to columns – Stack Overflow or sql – Postgres – Transpose Rows to Columns – Stack Overflow, but they are too complicated task to what I want to implement, and what I want to is pretty simple task.

For example, assume I have something like following one:

I want to convert the above to something like this:

The columns, tmp to tmp03, can be manually assigned for me, since the data I’m trying to is 4 or 5 of these, just simpler the sqi is the better.

Advertisement

Answer

You could use a pivot query with the help of ROW_NUMBER:

screen capture from demo link below

Demo

Advertisement