2
votes

I'm trying to use unpivot on a table. I'm using Workbench/J as a client to amazon redshift. The following select statement does not work:

SELECT 
  campaign_id,
  C.B,
  C.A
FROM campaign
UNPIVOT 
(
  A FOR B IN (item1, item2, item3)
) AS C

I get the following error:

ERROR: syntax error at or near "for" Position: 62 [SQL State=42601]

If possible, I'd like to use UNPIVOT rather than UNION and it doesn't recognize UNNEST as a function.

1
Are you sure it has an unpivot function? Not everything supports unpivot.Taryn

1 Answers

0
votes

The UNPIVOT syntax is correct however not every database supports the function.

You might want to consider using a UNION ALL:

select campaign_id, 'item1' as B, item1 as A
from campaign
union all
select campaign_id, 'item2' as B, item2 as A
from campaign
union all
select campaign_id, 'item3' as B, item3 as A
from campaign