oracle - How pivot row data as columns and fill data dynamically in SQL -
i have table column names exdescription,code,week00,qtysold. column week00 contains values such week1,week2,week3,week4 etc ... week53.
now, need write query print week00 values column names , values sum(qtysold). tried below, not getting result.
select * week_report1 pivot (sum (qtysold) week_00 in ([week1], [week2], [week3], [week4], [week5], [week6], [week7], [week8], [week9], ......... [week52], [week53])) maxin code in ('99');
i'm attaching excel pivot display of desired output better picture of want do.
oracle setup:
create table table_name ( exdescription varchar2(20), code int, week00 varchar2(6), qtysold int ); -- random data: insert table_name ( exdescription, code, week00, qtysold ) select dbms_random.string('x',10), 99, 'week' || level, round( dbms_random.value(0,101)-0.5) dual connect level <= 53; select * table_name; exdescription code week00 qtysold -------------------- ---------- ------ ---------- v01wlmuc50 99 week1 74 mt0hlg35fh 99 week2 53 qsxpkdndpo 99 week3 0 ...
query:
select * ( select code, week00, qtysold table_name code = 99 ) pivot ( sum( qtysold ) week00 in ( 'week1' week1, 'week2' week2, 'week3' week3 -- ... ) );
output
code week1 week2 week3 ---------- ---------- ---------- ---------- 99 74 53 0
Comments
Post a Comment