2016-03-04 44 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