Could you please kindly help to take a look at my error?
Original Table:
Target Table:
Use below code:
Select DATE,cities,sales
From Test
UNPIVOT
( sales
For cities in (city1, city2, city3)
) AS XYZ
and the result is successful.
However, the city list might grow into city4, city5... So I was trying to use dynamic unpivot to achieve this
Code:
DECLARE @cols AS NVARCHAR(MAX);
select @cols = COALESCE(@cols + ',', '') + quotename(COLUMN_NAME)
From ( Select column_name
FROM information_schema.columns
WHERE table_name = 'Test'
AND COLUMN_NAME <> 'Date' ) AS FiledList
Select DATE,cities,sales
From Test
UNPIVOT
( sales
For cities in (@cols)
) AS XYZ
There seems to be an error, and tried a couple ways, not working.