-1

I need to use pivot in postgres, below is the base table

enter image description here

Below is the desired output

enter image description here

Please help me with the query.

Utsav
  • 7,914
  • 2
  • 17
  • 38
Anshul
  • 43
  • 6
  • http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557 –  Mar 04 '16 at 13:46

2 Answers2

1

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
0

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/

Community
  • 1
  • 1
Utsav
  • 7,914
  • 2
  • 17
  • 38