I'm trying to transpose a few columns in postgresql.
From this data format:
deal id | deal name | create date | 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_ etc..
I want to transpose all columns apart from deal name and deal id and get 2 new columns (pipeline stage with all the stages: lead, warm lead, meeting 1, final meeting etc; and date) like this:
deal id | deal name | pipeline stage | data
I was able to do so with the following code:
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)
However, as we moved the data to redshift, this query no longer works and gives me this error
SQL Error [500310] [42883]: [Amazon](500310) Invalid operation: function values("unknown", timestamp without time zone) does not exist;
how can I get around this? I don't understand where the problem is
A typical work around uses 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
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.