Skip to Main Content
  • Questions
  • variable pivot XML to readable text in a select

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, William.

Asked: June 28, 2017 - 12:47 pm UTC

Last updated: June 29, 2017 - 2:27 am UTC

Version: 12.2.0.1.0

Viewed 10K+ times! This question is

You Asked

I have the following query

SELECT *
FROM (SELECT 1 Cnt, TO_NUMBER (TO_CHAR (A.Hiredate, 'YYYY')) Yr
FROM Emp A)
PIVOT xml
(SUM (Cnt) FOR Yr IN (any))

However I want to convert the XML which can have a variable number of columns into a readable format. I know that I can use XMLTABLE but it requires that you already know the number of columns. Is there any technique or option to allow me to do this?

Is there any plans to add the "any" clause into a standard pivot command so you could do something like

SELECT *
FROM (SELECT 1 Cnt, TO_NUMBER (TO_CHAR (A.Hiredate, 'YYYY')) Yr
FROM Emp A)
PIVOT
(SUM (Cnt) FOR Yr IN (any))

Thanks for any help you can give me.

and Connor said...

AMIS wrote a cool little tool to allow this

Take a look here

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


Rating

  (1 rating)

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

Comments

Exactly what I needed.

William Beilstein, June 29, 2017 - 2:32 am UTC

While it would be nice if Oracle would allow dynamic columns on the standard pivot, this will do the job that I needed. Thank you very much for the quick response.