Trying to convert my sql group by query to nested xml



I am working on a process to convert the OUTPUT result of an SQL statement to nested XML form.By nested,I mean is that all similar data should be grouped under same tag at the same level if the higher level are same for it. The XML should be something like this format:



<?xml version="1.0" encoding="UTF-8" ?>
<posFile> -- this is not a column
<fileFormat>4.00</fileFormat> -- this is not a column
<created>20140626</created> -- this is not a column
<pointInTime> -- this is not a column
<date>20140625</date>
<isSetl>1</isSetl>
<setlQualifier>final</setlQualifier>
<portfolio> -- this is not a column
<firm>123</firm>
<acctId>AAA12345</acctId>
<acctType>M</acctType>
<isCust>1</isCust>
<seg>N/A</seg>
<isNew>1</isNew>
<custPortUseLov>1</custPortUseLov>
<currency>USD</currency>
<ledgerBal>0</ledgerBal>
<ote>0</ote>
<securities>0</securities>
<lue>0</lue>
<ecPort> -- this is not a column
<ec>LKM</ec>
<ccPort> -- this is not a column
<cc>ED</cc>
<currency>USD</currency>
<pss>0</pss>
<np> -- this is not a column
<exch>LKM</exch>
<pfCode>ED</pfCode>
<pfType>FUT</pfType>
<pe>201409</pe>
<net>-5</net>
</np>
<np>
<exch>CME</exch>
<pfCode>ED</pfCode>
<pfType>FUT</pfType>
<pe>201412</pe>
<net>-6</net>
</np>
<np>
<exch>LKM</exch>
<pfCode>ED</pfCode>
<pfType>FUT</pfType>
<pe>201503</pe>
<net>-10</net>
</np>
</ccPort>
<ccPort> -- this is not a column
<cc>EE</cc>
-
-
-
</ccPort>
</ecPort>
</portfolio>
</pointInTime>
</posFile>


I tried using DBMS_XMLGEN, but I am not able to group the records based on the group by clause in the query.Then I have tried using seperate tags and XMLAGG, and here is the output that I created:



SELECT XMLSERIALIZE(CONTENT
XMLAGG(XMLELEMENT(posFile
,XMLFOREST(4 AS fileFormat,TO_CHAR (sysdate, 'yyyymmdd') AS created)
,(SELECT XMLAgg(
XMLELEMENT(pointInTime
,XMLFOREST(TAB.bus_date AS date1,1 AS isSetl,'final' AS setlQualifier)
,(SELECT XMLAgg(
XMLELEMENT(portfolio
,XMLFOREST(888 AS firm,TAB.acct AS acctId,'M' AS acctType,1 AS isCust,'N/A' AS seg,1 AS isNew,1 AS custPortUseLov,TAB.currency AS currency,0 AS ledgerBal,0 AS ote,0 AS securities,0 AS lue)
,(SELECT XMLAgg(
XMLELEMENT(ecPort
,XMLFOREST(TAB.span_ecport AS ec)
,(SELECT XMLAgg(
XMLELEMENT(ccPort
,XMLFOREST(TAB.futures_code AS cc,TAB.currency AS currency,TAB.strike_price AS pss)
,(SELECT XMLAgg(
XMLELEMENT(NP
,XMLFOREST(TAB.span_exch AS exch,TAB.span_pf AS pfCode,TAB.span_type AS pfType,TAB.ctym AS pe,TAB.qty AS net))) FROM TAB))) FROM TAB))) FROM TAB))) FROM TAB))) FROM TAB)
))
AS CLOB INDENT SIZE = 2) as XML
FROM TAB


However the the output is repeating and looks to be a cartesian product with each set containing all sets of lower lever:


Can someone help me to get this corrected


Any help would be appreciated.


No comments:

Post a Comment