Ik heb de volgende situatie. Ik heb de WDI GDF excel ingelezen in een SQL 2008 server.
De jaartallen staan nu in mijn sql in een kolom. Ik wil daar graag een row van maken zodat deze beter in een table later zijn te outputten.
Ik ben begonnen met deze query:
Daar heb ik via unpivot een nieuwe query van gemaakt:
resultaat:
Het probleem is nu dat ik niet verder kom met het omzetten van de series in kolommen. Ze staan nu onder elkaar als rows. Ik vermoed dat er een extra pivot of unpivot nodig is om de serie naam uit te splitsen in kolommen. Wie kan mij helpen met de volgende stap?
Het gewenste resultaat is:
De jaartallen staan nu in mijn sql in een kolom. Ik wil daar graag een row van maken zodat deze beter in een table later zijn te outputten.
Ik ben begonnen met deze query:
SQL:
1
2
3
| select [Series Name],[1971], [1972], [1973] from dbo.WDI_GDF_Data$ where [Series Code] IN ('EG.ELC.COAL.KH','EG.ELC.HYRO.KH','EG.ELC.LOSS.KH','EG.ELC.NGAS.KH','EG.ELC.NUCL.KH','EG.ELC.PETR.KH','EG.ELC.PROD.KH') AND [Country Code] = 'COL' |
Daar heb ik via unpivot een nieuwe query van gemaakt:
SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| SELECT [Series Name], [YEAR], [yearvalue] FROM (select [Series Name], [1970], [1971], [1972], [1973] from dbo.WDI_GDF_Data$ where [Series Code] IN ('EG.ELC.COAL.KH','EG.ELC.HYRO.KH','EG.ELC.LOSS.KH','EG.ELC.NGAS.KH','EG.ELC.NUCL.KH','EG.ELC.PETR.KH','EG.ELC.PROD.KH') AND [Country Code] = 'COL') p unpivot ([yearvalue] for [YEAR] in ( [1970], [1971], [1972], [1973] )) as unpvt |
resultaat:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| Series Name YEAR yearvalue Electricity production from coal sources (kWh) 1971 1302000000 Electricity production from coal sources (kWh) 1972 1297000000 Electricity production from coal sources (kWh) 1973 1416000000 Electricity production from hydroelectric sources (kWh) 1971 6663000000 Electricity production from hydroelectric sources (kWh) 1972 7298000000 Electricity production from hydroelectric sources (kWh) 1973 7939000000 Electric power transmission and distribution losses (kWh) 1971 1114000000 Electric power transmission and distribution losses (kWh) 1972 1512000000 Electric power transmission and distribution losses (kWh) 1973 1664000000 Electricity production from natural gas sources (kWh) 1971 944000000 Electricity production from natural gas sources (kWh) 1972 1026000000 Electricity production from natural gas sources (kWh) 1973 1011000000 Electricity production from nuclear sources (kWh) 1971 0 Electricity production from nuclear sources (kWh) 1972 0 Electricity production from nuclear sources (kWh) 1973 0 Electricity production from oil sources (kWh) 1971 1291000000 Electricity production from oil sources (kWh) 1972 878000000 Electricity production from oil sources (kWh) 1973 1230000000 Electricity production (kWh) 1971 10200000000 Electricity production (kWh) 1972 10499000000 Electricity production (kWh) 1973 11596000000 |
Het probleem is nu dat ik niet verder kom met het omzetten van de series in kolommen. Ze staan nu onder elkaar als rows. Ik vermoed dat er een extra pivot of unpivot nodig is om de serie naam uit te splitsen in kolommen. Wie kan mij helpen met de volgende stap?
Het gewenste resultaat is:
code:
1
2
3
| year, coal, oil, total enz 1972, aantal, aantal, aantal enz 1973, aantal, aantal, aantal enz |
Volgens mij zit je te lullen, want ik voel nattigheid....