Transform data from columns to rows on plsql oracle -
i have table 2 columns each month,
table1:
_____________________________________________________________________ |item | jan_p | jan_r | feb_p | feb_r | commpany| |______|___________|___________|____________|___________|____________ |1294 | 1234232 | 2323322 | 1242122 | 532323 | e987 | |______|___________|___________|____________|___________|___________| i need transfer data new, more normalised table this:
table2:
________________________________________________ | item | month | cost_p | cost_r | company | |_______|_______|_________|_________|__________| | 1294 | 1 |1234232 | 2323322| e987 | | 1295 | 2 |1242122 | 532323 | e987 | |_______|_______|_________|_________|__________| see image https://ibb.co/cdm91q
i coded loop iterating on first table nested loop insert row each month this:
for reg in (select * table1) loop x in 1..12 loop case x when 1 insert table2(..., mes , ...) values(... , x, ...) when 2 insert table2(..., mes , ...) values(... , x, ...) this working wanted know if there way achieve same outcome.
"i wanted know if there way achieve "
yes there is. ideal use case insert syntax.
insert table2 (item, month, cost_p, cost_r, commpany ) values (item, 1, jan_p, jan_r, commpany) table2 (item, month, cost_p, cost_r, commpany ) values (item, 2, feb_p, feb_r, commpany) table2 (item, month, cost_p, cost_r, commpany ) values (item, 3, mar_p, mar_r, commpany) table2 (item, month, cost_p, cost_r, commpany ) values (item, 4, apr_p, apr_r, commpany) table2 (item, month, cost_p, cost_r, commpany ) values (item, 5, may_p, may_r, commpany) table2 (item, month, cost_p, cost_r, commpany ) values (item, 6, jun_p, jun_r, commpany) .... select * table1 / multi-table inserts allow neat things still using set operations. find out more.
Comments
Post a Comment