Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SELECT ... PIVOT .. ORA-00936

pnoskoAug 4 2011 — edited Aug 4 2011
Oracle database: 11.2.0.2.0

I'm trying to pivot a query that returns parameter names/values for a given key into a row that lists the key and then a column for each pair where the parameter name becomes the column name. The 11g SQL reference indicates that the pivot_in_clause can take a subquery. So I was trying to use the subquery to avoid hard-coding the column names, to no avail.

Given...

KEY PNAME PVALUE
10 P1 V1
10 P2 V2
10 P3 V3

I would like the query to return...

KEY P1 P2 P3
10 V1 V2 V3

I can do this with the following query...

WITH pivot_data AS ( SELECT * FROM pnosko_pivot WHERE key = 10 ORDER BY pname )
SELECT *
FROM pivot_data
PIVOT ( MAX(pvalue)
FOR pname
IN ('P1' as p1,'P2' as p2,'P3' as p3) );

But if I try and use a subquery as follows (to allow for new parameter rows to be handled dynamically), I get an ORA-00936.

WITH pivot_data AS ( SELECT * FROM pnosko_pivot WHERE key = 10 ORDER BY pname )
SELECT *
FROM pivot_data
PIVOT ( MAX(pvalue)
FOR pname
IN (SELECT DISTINCT pname FROM pnosko_pivot WHERE key = 10 ORDER BY 1) );

Could someone point to my problem, interpretation or otherwise? Thanks.
This post has been answered by Frank Kulash on Aug 4 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 1 2011
Added on Aug 4 2011
3 comments
1,983 views