I'm using pivot in Oracle PL SQL Developer as follows:
SELECT *
FROM population
PIVOT (AVG(Total) for Data_Type IN ('Group1','Group2','Group3'))
This works fine, but I don't want to have to edit every time a new column is added or one is changed (i.e. Group4, 5, 6 etc), so I tried a sub-query as follows:
SELECT *
FROM population
PIVOT (AVG(Total) for Data_Type IN (SELECT Data_Type FROM population))
This results in the following error: ORA-00936: missing expression.
After some research, it appears that I can generate the results with XML, so I tried the following:
SELECT *
FROM population
PIVOT XML(AVG(Total) for Data_Type IN (ANY))
This actually generates the desired data, but in XML format. So my question is, how can I convert the XML results into standard table format within PL SQL Developer? Or, if I want to bring the generated XML file into a tool like Crystal Reports, I need to have a schema file for these results. Is that something that can easily be auto generated within the SQL?
Would you consider using PIPELINED function to achieve your goal?
I have written a an example of such a function. The example is based on the table, sample data and
PIVOT
query from Tom Kyte's articles which you can find on his site:Tom Kyte's article about PIVOT/UNPIVOT
Tom Kyte's article about PIPELINED functions
The example works as follows.
We create two types:
Then we create a PIPELINED function which contains the query with
PIVOT
, which generates XML (so you do not have to hard-code the values you want to pivot over). This function extracts data from generated XML and passes (PIPEs) rows to the calling query as they are generated (on the fly - they are not generated all at once which is important for performance).Finally, you write a query which selects records from that function (at the end is an example of such a query).
Output:
You can generate the text of your first SQL statement by iterating, then separately execute that statement.
If you don't mind a quasi-dynamic solution, you might schedule the creation of a VIEW in this manner using dynamic SQL (i.e. EXECUTE IMMEDIATE).
(A Crystal Report, to my knowledge, would need to know the column names in advance.)
Edited to add code. I didn't test this. Note, too, that this will break when the SQL statement exceeds 32KB, regardless of the actual number of multi-byte characters.