1

我正在尝试在 postgresql 中转置几列。

从此数据格式:

交易编号 | 交易名称 | 创建日期 | Lead_date_pipeline_ | warm_lead_date_pipeline_ | d.meeting_1_date_pipeline_ | d.final_meeting_date_pipeline_ | d.contract_sent_date_pipeline_ | d.closed_won_date_pipeline_ 等。

我想转置除交易名称和交易 ID 之外的所有列,并获得 2 个新列(具有所有阶段的管道阶段:领导、热情领导、会议 1、最终会议等;和日期),如下所示:

交易编号 | 交易名称 | 管道阶段| 数据

我可以使用以下代码做到这一点:

SELECT d."deal id", d."deal name", v.*
FROM   gg_deals d
     , LATERAL (
   VALUES
      ('create date', d."create date")
    , ('lead_date_pipeline_', d.lead_date_pipeline_)
    , ('warm_lead_date_pipeline_', d.warm_lead_date_pipeline_)
    , ('meeting_1_date_pipeline_', d.meeting_1_date_pipeline_)
    , ('final_meeting_date_pipeline_', d.final_meeting_date_pipeline_)
    , ('contract_sent_date_pipeline_', d.contract_sent_date_pipeline_)
    , ('closed_won_date_pipeline_', d.closed_won_date_pipeline_)
    , ('closed_lost_date_pipeline_', d.closed_lost_date_pipeline_)
   ) v (Pipeline_stage, Date)

但是,当我们将数据移动到 redshift 时,此查询不再有效并给我这个错误

SQL Error [500310] [42883]: [Amazon](500310) Invalid operation: function values("unknown", timestamp without time zone) does not exist;

我怎样才能解决这个问题?我不明白问题出在哪里

4

1 回答 1

0

一个典型的解决方法使用union all

select deal_id, deal_name, 'create date' as pipeline_stage "create date" as date from gg_deals
union all select deal_id, deal_name, 'lead_date_pipeline_', d.lead_date_pipeline_ from gg_deals
union all select deal_id, deal_name, 'warm_lead_date_pipeline_', d.warm_lead_date_pipeline_ from gg_deals
union all select deal_id, deal_name, 'meeting_1_date_pipeline_', d.meeting_1_date_pipeline_ from gg_deals
...
于 2020-11-09T17:45:43.940 回答