Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top