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!

conditional XMLAGG

GregVFeb 14 2024

Hi All,

I'm not very skilled at XQuery so I'm asking here because I suppose there's a better way to achieve my requirement.

I need to build xml data from a query, splitting the rows into 3 differents blocks against given values. In each block I'm using XMLAGG because I need to group the values into a list.

Here's what I've written. It gives me the expected result, but I'm suspecting a more efficient way that querying 3 times the source table? In other words, is there a way to “condition” XMLAGG against certain column values?

with v(criteria_id, criteria_value)
as (select 11, 131 from dual union all
   select 11, 113 from dual union all
   select 12, 2 from dual union all
   select 15, 7 from dual union all
   select 15, 10 from dual union all
   select 16, 13 from dual union all
   select 17, 1000 from dual 
)
select xmlelement("root",
                   xmlelement("main_block", 
                                     xmlelement("block1",
                                                xmlelement("criteria_list",
                                                           (select xmlagg(xmlelement("criteria", 
                                                                                     xmlelement("id", criteria_id),
                                                                                     xmlelement("value", criteria_value)
                                                                                    )
                                                                          order by criteria_id             
                                                                         )
                                                            from v 
                                                            where criteria_id = 11 
                                                           )
                                                          )
                                               ),
                                     xmlelement("block2",
                                                xmlelement("criteria_list",
                                                           (select xmlagg(xmlelement("criteria", 
                                                                                     xmlelement("id", criteria_id),
                                                                                     xmlelement("value", criteria_value)
                                                                                    )
                                                                          order by criteria_id             
                                                                         )
                                                            from v 
                                                            where criteria_id = 12
                                                           )
                                                          )
                                               ), 
                                     xmlelement("block3",
                                                xmlelement("criteria_list",
                                                           (select xmlagg(xmlelement("criteria", 
                                                                                     xmlelement("id", criteria_id),
                                                                                     xmlelement("value", criteria_value)
                                                                                    )
                                                                          order by criteria_id             
                                                                         )
                                                            from v 
                                                            where criteria_id in (15, 16, 17)
                                                           )
                                                          )
                                               )                
                                    )
                )                    
from dual;    

My version is 19.13 EE.

Thanks

This post has been answered by cormaco on Feb 14 2024
Jump to Answer
Comments
Post Details
Added on Feb 14 2024
4 comments
260 views