I have GDP data for 1960 - 2020, each year is stored as a column. In order to unpivot the columns, do I have to hard code all year(columns)?
For example
| country name | 1960 | 1961 |
|---|---|---|
| US | 200 | 400 |
| CANADA | 300 | 400 |
Desired(unpivot)
| countryname | value | Year |
|---|---|---|
| US | 200 | 1960 |
| US | 400 | 1961 |
| CANADA | 300 | 1960 |
| CANADA | 400 | 1961 |
But doing this for 1960- 2020, is it necessary to state each year columns in my unpivot statement?
I'm attempting to use dynamic query and at a very starting point, I have
DECLARE @DynamicSQL VARCHAR(MAX)
DECLARE @year INT = 1960
SET @DynamicSQL = 'SELECT GDP.[' + CAST(@year AS VARCHAR(10)) +'] FROM GDP'
EXEC(@DynamicSQL)
But how can I increment 1 year and add the list of years in one SET statement?
Thanks!