2016-03-04 43 views
-1

我需要在Postgres一起使用透视使用数据透视,下面是基表如何Postgres的

enter image description here

下面是所需的输出

enter image description here

请帮我查询。

+0

http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking - 问题/ 285557#285557 –

回答

1

这实际上是一个非支点,没有支点

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

非常感谢你..救了我 – Anshul

0

如果只有3,您需要转动柱子,然后用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 

如果列的数量是动态的,那么我建议你使用动态数据透视表。

Dynamic pivot query using PostgreSQL 9.3

http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/

+0

谢谢你Utsav – Anshul