【问题标题】:How to use Pivot in postgres如何在 postgres 中使用 Pivot
【发布时间】:2016-03-04 13:41:32
【问题描述】:

我需要在postgres中使用pivot,下面是基表

下面是想要的输出

请帮我查询。

【问题讨论】:

标签: sql postgresql pivot crosstab


【解决方案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

【讨论】:

  • 非常感谢..救了我
【解决方案2】:

如果您只需要旋转 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多