Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to get the data using Pivot dynamically but not able to get if possible then please let me know i'm sending my query here below

SELECT * FROM(
                SELECT ROW_NUMBER() OVER (PARTITION BY IAI_COVERNUMBER,IAI_VERNO ORDER BY IAI_COVERNUMBER) AS RNO, IAI_COVERNUMBER AS INSGRP 
                FROM IAI_ACTUALINSURERS INNER JOIN PLY_POLICY ON  (IAI_COVERNUMBER=PLY_COVERNUMBER AND IAI_VERNO=PLY_VERNO)
                WHERE IAI_COVERNUMBER in ('0011520','0010891') 
                
            ) PIVOT (MIN(INSGRP)--,MIN(IAI_INSPROPORTION) AS INSPROP              
              FOR RNO IN (1,2,3,4,5));


here i've set RNO hard-coded but i want it the number of rows exist
SELECT ROW_NUMBER() OVER (PARTITION BY IAI_COVERNUMBER,IAI_VERNO ORDER BY IAI_COVERNUMBER) AS RNO



But i'm also using Pivot XML but it is giving me output in XML sting in a single column
<PivotSet>>item><column name RNO= >1</column><column name = INSGRP>BAGIC-B02-100-OG08190133030000052</column></item><item><column name = RNO>2</column><column name = INSGRP></column></item></PivotSet></i></b>

but i want INSGRP values in no. of columns as above that will give us 2 columns ....it may be more than 2 columns


Please provide the solution if anybody has face or know solution.

What I have tried:

above i mentioned everything what i want and have tried
Posted
Updated 4-Sep-20 11:27am

Not sure if I fully understand your question but if you mean that you want to have dynamic amount of columns in PIVOT clause then you need to build the SQL statement dynamically.

In other words depending on the situation if you need to have
SQL
PIVOT (MIN(INSGRP) FOR RNO IN (1,2,3,4,5));
or
SQL
PIVOT (MIN(INSGRP) FOR RNO IN (1,2,3,4,5,6,7,8,9));
or
SQL
PIVOT (MIN(INSGRP) FOR RNO IN (1,2,3));

You need different SQL statements since pivoting does not support dynamic values. See Ask Tom "Is it possible to use dynamic query in PIVOT in cl..."[^]
But if you use Pivot XML then you can use a subquery for the columns. For an example have a look at Dynamic pivot in oracle sql - Stack Overflow[^]
 
Share this answer
 
I'm a little shocked that there are not more answers on the internet to this conundrum.

PIVOT XML is pretty useless if you cannot convert the XMLTYPE string to individual columns like you can with just a regular pivot. I believe that is the question that Original Poster is asking.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900