【发布时间】:2012-10-29 18:59:14
【问题描述】:
可能重复:
Transposing an sql result so that one column goes onto multiple columns
我想在我的 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 中可行吗?
【问题讨论】:
-
如果您搜索术语pivot,您会发现很多结果,不一定与postgres 相关。请注意,Oracle 和 SQL Server 实际上实现了
PIVOT子句,而不是 Postgres -
查看tablefunc模块中的交叉表函数:postgresql.org/docs/current/static/tablefunc.html
-
查看:github.com/jumpstarter-io/colpivot 了解动态解决方案。
标签: sql postgresql pivot