可能重複:
Transposing an sql result so that one column goes onto multiple columns如何轉置PostgreSQL中的列和行(即,如何切換行和列)?
我想要做一個排序行/列交換的在我的PSQL數據庫。下面是我的示例數據庫:
id place price year
1 U.S. 80 2000
2 U.S. 60 2001
3 U.S. 40 2002
4 U.K. 600 2000
5 U.K. 500 2001
6 U.K. 350 2002
我想那個表格轉換爲下面的例子:
year U.S. U.K.
2000 80 600
2001 60 500
2002 40 350
這可能在PostgreSQL的?
如果您搜索術語[tag:piv],您會發現很多結果,不一定與postgres相關。 OT。請注意,Oracle和SQL Server實際上實現了'PIVOT'子句,但不是Postgres –
查看tablefunc模塊中的交叉表函數:http://www.postgresql.org/docs/current/static/tablefunc.html –
請參閱: https://github.com/jumpstarter-io/colpivot獲取動態解決方案。 –