2015-02-27

postgresql unpivot

Given a monthly currency conversion table like:
iso4127_codeJanFebMarAprMayJunJulAugSepOctNovDec
JPY0.0096130.0097920.0097780.0097570.0098270.0097900.0098290.0097170.0093460.0092620.0086060.008375

I need to unpivot so that I get a table that is:
iso4127_codemonthrate
JPY10.009613
JPY20.009792
...
select iso4127_code, 
unnest(array[1,2,3,4,5,6,7,8,9,10,11,12]) as month,
unnest(array[jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec]) as rate
from table

No comments:

Post a Comment