If UNPIVOT
is available, you should definitely use that. For earlier versions of Oracle you could cross join your table with a table of the month names (generated or prebuilt) and then use decode or case statements to select the correct month, price, and quantity. Here is how this would look.
create table prices (Year Varchar2(4), JanPrc Number(3), JanQty Number(3),
FebPrc Number(5,2), FebQty Number(3), MarPrc Number(3), MarQty Number(3));
insert into prices values ('2008',1,500,1,600,1,700);
insert into prices values ('2009',50,100,20,300,30,800);
insert into prices values ('2010',60,5,70,10,80,15);
SELECT Year, Month, DECODE(MonthNumber,1,JanPrc,2,FebPrc,MarPrc) Price,
DECODE(MonthNumber,1,JanQty,2,FebQty,MarQty) Quantity
FROM Prices
CROSS JOIN (
SELECT rownum MonthNumber,
to_char(to_date(to_char(rownum,'FM00') || '2000','MMYYYY'),
'FMMonth') Month
FROM dual CONNECT BY rownum <= 3
)
ORDER BY Year, MonthNumber;
2010janprc, 2010janqty ... 2011janprc, 2011janqty...
. :)unpivot
?