I need to use pivot in postgres, below is the base table
Below is the desired output
Please help me with the query.
I need to use pivot in postgres, below is the base table
Below is the desired output
Please help me with the query.
This is actually an un-pivot, not pivot
select year, week, 'loading' as area, loading as value
from the_table
union all
select year, week, 'picking', picking
from the_table
union all
select year, week, 'painting', painting
from the_table
If you have only 3 columns which you need to pivot, then use union
select year,week,'loading' as aread,loading as val from tbl
union all
select year,week,'painting' as area,painting as val from tbl
union all
select year,week,'picking' as area,picking as val from tbl
If the number of column is dynamic, then I 'd suggest you to use dynamic pivot.
Dynamic pivot query using PostgreSQL 9.3
http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/