Wednesday, 15 April 2015

oracle11g - using sql pivot -



oracle11g - using sql pivot -

i using oracle 11gr2 running on windows server 2008 r2

i have table this

year code value ---- ---- ----- 1991 1 v1 1991 2 v2 1991 3 v3 1992 1 v4 1992 2 v5 1992 3 v6 ... 2050

the code column can 1 through 10.

i see final output (columns having value 1 through 10)

year 1 2 3 4 .... 10 ---- -- -- -- -- -- 1991 v1 v2 v3 null.......null 1992 v4 v5 v6. ...etc

how can pivoting result in above format? in advance.

one easy way without using pivot using case:

select year, min(case when code = 1 value end) col1, min(case when code = 2 value end) col2, min(case when code = 3 value end) col3, min(case when code = 4 value end) col4, min(case when code = 5 value end) col5, min(case when code = 6 value end) col6, min(case when code = 7 value end) col7, min(case when code = 8 value end) col8, min(case when code = 9 value end) col9, min(case when code = 10 value end) col10 yourtable grouping year

with pivot, this:

select * (select year, code, value yourtable) pivot (min(value) code in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) );

sql oracle11g pivot

No comments:

Post a Comment