sql - query without union pivot select -
hello have query below execution time long. please introduced me try different solution .there query(oracle 10g)
select * (with t (select cor_id, cor_cod, wei1, days, 'wei1' wei_type (table)) select * t pivot(max(wei1) for(days) in('01' day_01, '02' day_02, '03' day_03, '04' day_04, '05' day_05, '06' day_06, '07' day_07, '08' day_08))) union (with t (select cor_id, cor_cod, wei2, days, 'wei2' wei_type (table)) select * t pivot(max(wei2) for(days) in('01' day_01, '02' day_02, '03' day_03, '04' day_04, '05' day_05, '06' day_06, '07' day_07, '08' day_08))) union (with t (select cor_id, cor_cod, wei3, days, 'wei3' wei_type (table)) select * t pivot(max(wei3) for(days) in('01' day_01, '02' day_02, '03' day_03, '04' day_04, '05' day_05, '06' day_06, '07' day_07, '08' day_08)))
how change query without union ?
that result this: that result this:
to this: to this:
please me
you have table several wei columns , separate rows days. , want result several day columns , separate rows weis instead. transformation rows columns and columns rows.
your query looks appropriate task. only, using union should union all. union all glues records together, want. union same looks duplicates remove. 3 partial queries don't produce duplicates - differ @ least in wei_type. change union union all, dbms doesn't have work, should result in faster query.
and here alternative query neither using union nor union all on table. cross joins weis produce these rows , aggregates/pivots day rows columns.
select cor_id, cor_cod, wei_type, day_01, day_02, day_03, day_04, day_05, day_06, day_07, day_08 ( select t.cor_id, t.cor_cod, t.days, w.wei_type, case w.wei_type when 'wei1' t.wei1 when 'wei2' t.wei2 else t.wei3 end wei thetable t cross join ( select 'wei1' wei_type dual union select 'wei2' wei_type dual union select 'wei3' wei_type dual ) w ) pivot ( max(wei) for(days) in ( '01' day_01, '02' day_02, '03' day_03, '04' day_04, '05' day_05, '06' day_06, '07' day_07, '08' day_08 ) ) order cor_id, cor_cod, wei_type; and here same conditional aggregation instead of pivot same.
select cor_id, cor_cod, wei_type, max(case when days = '01' wei end) day_01, max(case when days = '02' wei end) day_02, max(case when days = '03' wei end) day_03, max(case when days = '04' wei end) day_04, max(case when days = '05' wei end) day_05, max(case when days = '06' wei end) day_06, max(case when days = '07' wei end) day_07, max(case when days = '08' wei end) day_08 ( select t.cor_id, t.cor_cod, t.days, w.wei_type, case w.wei_type when 'wei1' t.wei1 when 'wei2' t.wei2 else t.wei3 end wei thetable t cross join ( select 'wei1' wei_type dual union select 'wei2' wei_type dual union select 'wei3' wei_type dual ) w ) group cor_id, cor_cod, wei_type order cor_id, cor_cod, wei_type;
Comments
Post a Comment