Skip to Main Content
  • Questions
  • Dynamic Values list in PIVOT IN CLAUSE

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Akram.

Asked: November 30, 2018 - 6:27 am UTC

Last updated: March 13, 2020 - 12:04 pm UTC

Version: Oracle 11G

Viewed 10K+ times! This question is

You Asked

Dear Developers,
I want to write a PIVOT/SQL Statement to get the values in PIVOT IN-Clause Dynamically like below:

Select * FROM
(Select StaffID, OSSID FROM Staff)
PIVOT(Count(StaffID) FOR OSSID IN(Select OSSID FROM Staff));

Error Message is ORA-00936: missing expression.
Please check the syntax and let me know how to fix for above situation

and Connor said...

We don't support it directly, but people have come up with ways to workaround this limitation.

Examples here:

https://technology.amis.nl/2006/05/24/dynamic-sql-pivoting-stealing-antons-thunder/

https://stewashton.wordpress.com/2018/05/28/generic-pivot-function/

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Answer might be out of date

Himanshu Rana, March 13, 2020 - 7:29 am UTC

I have tried to use the syntax provided in the link, but i am executing the below query i am getting an error

select *
from ( pivot
(' SELECT
PD.NAME AS DEPT_NAME,
PJFV.NAME AS JOB_NAME,
COUNT(PAPF.PERSON_NUMBER) AS SID

FROM
PER_ALL_PEOPLE_F PAPF,
PER_ALL_ASSIGNMENTS_M PAAM,
PER_JOBS_F_VL PJFV,
PER_DEPARTMENTS PD

WHERE
PAPF.PERSON_ID = PAAM.PERSON_ID
AND PAAM.JOB_ID = PJFV.JOB_ID
AND PAAM.ORGANIZATION_ID = PD.ORGANIZATION_ID
AND PAAM.ASSIGNMENT_TYPE = 'E'
AND PAAM.PRIMARY_FLAG = 'Y'
AND PAAM.PRIMARY_ASSIGNMENT_FLAG = 'Y'
AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE AND PAAM.EFFECTIVE_END_DATE
GROUP BY PD.NAME,PJFV.NAME '
)
)

Error is "ORA-00907: missing right parenthesis"
Chris Saxon
March 13, 2020 - 12:04 pm UTC

Did you create the PIVOT function too?

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.