I have a table that has products defined by an identifying code that begins with a number and ends with a letter such as 1A
, 3B
, 7C
, etc. The query assembles the data I need about these with a PIVOT
statement. Unfortunately, in Oracle we are not allowed to begin aliases with a number. So far, I have successfully made the query work around that by outputting data that is essentially the following:
SELECT * FROM MyTable
PIVOT ( COUNT(Product) FOR ProductName IN (&product_statement) )
where the substitution variable product_statement
was dynamically generated with the assistance of the same process described here: How do I set LINESIZE and PAGESIZE with a substitution variable?. This is a representative result:
NAME | A1 | B3 | C7
Product A| 1 | 2 | 1
Product B| 3 | 1 | 2
Now I am trying to turn those product headings back around. Again with the substitution variable process, I created another variable named &column_statement
that looks like this (each line is separated by CHR(10)
):
COLUMN A1 TEMP FORMAT A3 HEADING ‘1A’
COLUMN B3 TEMP FORMAT A3 HEADING ‘3B’
COLUMN C7 TEMP FORMAT A3 HEADING ‘7C’
My thought process was that I could do the following in SQL*Plus:
&column_statement
SELECT * FROM MyTable
PIVOT ( COUNT(Product) FOR ProductName IN (&product_statement) )
/
but it gives me the following:
SP2-0734: unknown command beginning “&column_st…” – rest of line ignored.
NAME | A1 | B3 | C7
Product A| 1 | 2 | 1
Product B| 3 | 1 | 2
What do I have to do to dynamically set the column headers?