sql - Using pivot on multiple columns of an Oracle row -


i have following sample data in oracle table (tab1) , trying convert rows columns. know how use oracle pivot on 1 column. possible apply multiple columns?

sample data:

type  weight  height       50      10       60      12   b     40      8   c     30      15   

my intended output:

a-count b-count c-count a-weight b-weight c-weight a-height b-height c-height   2       1       1       110      40       30       22       8        15   

what can do:

with t  (select type, weight tab1 ) select * t pivot ( count(type) type in (a, b, c, d,e,f) )   

the above query gives me below result

a b c   2 1 1   

i can replace count(*) sum(weight) or sum(height) pivot height or weight. looking do, can't do, pivot on 3 (count, weight , height) in 1 query.

can done using pivot?

as the documentation shows, can have multiple aggregate function clauses. can this:

select * (   select * tab1 ) pivot (   count(type) ct, sum(weight) wt, sum(height) ht   type in ('a' a, 'b' b, 'c' c) );  a_ct a_wt a_ht b_ct b_wt b_ht c_ct c_wt c_ht ---- ---- ---- ---- ---- ---- ---- ---- ----    2  110   22    1   40    8    1   30   15  

if want columns in order showed add level of subquery:

select a_ct, b_ct, c_ct, a_wt, b_wt, c_wt, a_ht, b_ht, c_ht (   select * (     select * tab1   )   pivot (     count(type) ct, sum(weight) wt, sum(height) ht     type in ('a' a, 'b' b, 'c' c)   ) );  a_ct b_ct c_ct a_wt b_wt c_wt a_ht b_ht c_ht ---- ---- ---- ---- ---- ---- ---- ---- ----    2    1    1  110   40   30   22    8   15  

sql fiddle.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -