I am writing a stored procedure for which I need to populate a table based on the data being reported on.
In this situation, I will be pulling in three values per day for a certain code in a date range.
Say on a certain run of this stored procedure, I have code values X, Y, and Z for a date range as so:
select abc.code,
abc.date,
abc.val_1,
abc.val_2,
abc.val_3
from data.abc
where abc.date BETWEEN '01-OCT-2009' AND '31-OCT-2009'
So for each day in the date range, I have three records for codes x,y, and z.
In my final table, I need to transform this from rows to columns. Normally I would use the decode function, but here I want to create my final table dynamically based on the data coming back.
In this case I would have one record for each day in the range and have 9 more columns (val_1_X, val_2_x, val_3_x, val_1_y, and so on).
I would like to set this up dynamically so that I do not need to re-open my stored procedure when a new "code" is introduced and so that on each instance of the report, only the "codes" being returned on that instance of the report are included on the final table.
Is this possible through dynamic sql? I am on Oracle version 10g.