Can a Pivot table be created in Oracle 10g?

5 pts.
Tags:
Oracle 10g
Pivot tables
I am running a stored procedure on ORACLE 10g that truncates and repopulates a table (tbl_A) that contains 2 columns; a list of unique codes and monetary amounts. The number of codes may vary weekly. I would like to create a second table(tbl_B) that contains a column for each of the names being taken from each of these codes with their monetary amount in that column. Can this be done?

Tbl_A UNIQUE CODES Amount AAA 100.00 BBB 150.00 CCC 200.00 Tbl_B AAA BBB CCC 100.00 150.00 200.00

Answer Wiki

Thanks. We'll let you know when a new response is added.

Yes, but in 10g it is not as easy as it would be in 11g where the PIVOT clause was introduced.

You could do it using the DECODE function, but you would need to know all the possible codes to construct the query.

Something like this:

<pre>SELECT MAX(DECODE(code,'AAA',amount,NULL)) AAA,
MAX(DECODE(code,'BBB',amount,NULL)) BBB,
MAX(DECODE(code,'CCC',amount,NULL)) CCC
FROM codes;</pre>

Since the number of codes may vary, you would probably need to construct this query dynamically inside your procedure.

Also, you could use the MODEL clause, as suggested in the following article:

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: