[MS SQL] Unpivot een tabel

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • MoBi
  • Registratie: Oktober 1999
  • Laatst online: 14-09 09:44
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:
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....


Acties:
  • 0 Henk 'm!

  • PolarBear
  • Registratie: Februari 2001
  • Niet online
Ik zou het gewoon nog een keer importeren met een mooi SSIS package en de data zo oplsaan zoals je het later nodig hebt. Scheelt een hoop moeilijke queries.

Acties:
  • 0 Henk 'm!

  • MoBi
  • Registratie: Oktober 1999
  • Laatst online: 14-09 09:44
Maar er zouden toch ook andere oplossingen mogelijk moeten zijn als een ssis package?

Volgens mij zit je te lullen, want ik voel nattigheid....


Acties:
  • 0 Henk 'm!

  • _js_
  • Registratie: Oktober 2002
  • Laatst online: 18-08 21:31
MoBi schreef op woensdag 02 februari 2011 @ 19:40:
Maar er zouden toch ook andere oplossingen mogelijk moeten zijn als een ssis package?
Ja, natuurlijk. Toen je kolommen naar rijen wou hebben gebruikte je unpivot. Nu wil je rijen naar kolommen, raad eens wat je nodig hebt.

Acties:
  • 0 Henk 'm!

  • MoBi
  • Registratie: Oktober 1999
  • Laatst online: 14-09 09:44
Kan er een aanzetje worden gegeven van waar ik de pivot moet toevoegen? Dan kan ik weer verder met het uitwerken.

Volgens mij zit je te lullen, want ik voel nattigheid....