Avatar of nssudha
nssudha asked on

Oracle XML Hierarchical output.

Hi,

I need to create a hieararchical XML from oracle table data, For Example: EMP. In my actual scenario, the entire data is coming from a view. In the EMP case, it is coming from Employee table.

Please find attached the sample xml output file. I masked my original data and may not look like a typical XML file. Basically, I need to group all the employees under one department and subgroup them under one MGR. I was able to generate the XML without the grouping. For example if there are 3 employees under deptno 10 and MGR 7839. Deptno 10 and MGR 7839 are also repeated for each employee.

Please help me with the query to generate the xml.

Thanks in advance.

Sudhkaar Naraparaju.
QualityData-Raj.xml
Oracle Database

Avatar of undefined
Last Comment
nssudha

8/22/2022 - Mon
slightwv (䄆 Netminder)

Any reason you don't add deptno and empno as attributes?

Also not getting the need for the H node.

You really don't have a parent child relationship in the XML you posted.  Is the attached file what you have or what you are expecting as a final result?
ASKER
nssudha

As I said, it is all coming from a view. The data is coming from 3-4 multiple tables. I combined them all into one view using UNION ALL. I am expecting a similar file as the final result.

Basically, I need to keep a main group as deptno and under that I need to have a subgroup of MGR and under that I need to have another subgroup of empsalary which is what the H node is.

Thanks,
Sudhakar.
slightwv (䄆 Netminder)

>>subgroup of MGR and under that I need to have another subgroup of empsalary

But that isn't what you show in the sample you posted.  MGR and E nodes are peers.  They are both children of EMPINFO.

So you want E nodes to be children of MGR?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
nssudha

Yes. I want E nodes to be the children of MGR. I might have made a mistake while masking the XML.

Thanks,
Sudhakar.
Sean Stuber

I might have made a mistake while masking the XML.

if so, can you post a corrected XML?
ASKER
nssudha

Hi sdstuber,

It's baskically a hiearchical XML with Dept as the main node, MGR as the sub node and E as it's sub node.

Thanks,
Sudhakar.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
nssudha

I am attaching what might be a better XML than the first one (I hope). Please let me know if there any questions.

Thanks,
Sudhakar.
QualityData2.xml
slightwv (䄆 Netminder)

Since your expected results are loosely based on your actual query, I'm not going to re-write the example from the link below to tweak it for what isn't exactly what you need.

It is a good example also based off the EMP table and shows the basics of a hierarchical XML generation.

https://forums.oracle.com/message/1447865

Snippet from that link to pay attention to:
select xmlelement("employees",
  (select dbms_xmlgen.getxmltype(dbms_xmlgen.newcontextFromHierarchy('
    select level
           , xmlelement("emp"
           , xmlelement("number", empno)
           , xmlelement("name", ename)
           , xmlelement("salary", sal)
           , xmlelement("hiredate", hiredate)
           , xmlelement("level", level)
           )
    from emp
    where deptno = 20
    start with mgr is null connect by prior empno=mgr
    order siblings by hiredate
  '))
  from dual)
  ) xmldoc
from dual

Open in new window

ASKER
nssudha

I saw this example in OTN before I posted here. My situation is a little different. I do not have columns similar to EMPNO and MGR in my tables or view to satisfy the condition connect by prior  EMPNO=MGR.

My data is medical data.
Hospital Code replaces DEPTNO.
Medical Code replaces MGR
Medical Details replaces the info under node "E"
Medical Details include "Medicine Name", "Amount","Date" etc.

Please let me know how to tackle this.

Thanks,
Sudhakar.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
slightwv (䄆 Netminder)

We can only post based on what you provide.  You asked for a hierarchical XML example based on the EMP table based on a MGR EMP relationship.

Please post a more accurate example and expected results.

Dummy up some sample tables and data and based on your actual requirements and expected results.  Then we can build a test case based of that with working code.

The closer you make your example test case, the closer our code will be to a copy/paste solution.
ASKER
nssudha

Sure. I will work on it and post it soon.

Thanks,
Sudhakar.
ASKER
nssudha

Please find attached the data (I put the datatypes right below the column names) and the XML for my data. It is as close to the data I have in my DB with the actual data masked. I put the dates and most of the other details the same except for the top nodes.

Hope these are better samples.

Thanks,
Sudhakar.
MyData.xml
MedicalData.xls
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
nssudha

I kind of got an output by following the query in the link below. I am not able to group the results to get the count for the number of records in the output.

Please let me know if you have a way to get the count also.

Thanks,
Sudhakar.

<link removed - GaryC123>
slightwv (䄆 Netminder)

I'll try to get some time later today to work on this.
ASKER
nssudha

Thank you slightwv.

Thanks,
(Raj) Sudhakar.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
slightwv (䄆 Netminder)

>>Please let me know if you have a way to get the count also.

I showed you this in your previous question:
https://www.experts-exchange.com/Database/Oracle/Q_28269030.html#a39580824

In looking at your posted sample data, I'm not seeing a hierarchical layout in the data.  Where is the parent/child relationship?

Based on the data and expected results you posted, any SQL I came up with would be real similar to the same SQL I posted in the link above from your previous question.

Also, in the latest expected results you show a count of 5.  What is that 5 a count of in the sample data?
ASKER CERTIFIED SOLUTION
Sean Stuber

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Sean Stuber

I didn't include formatting in my queries,  you can use xmlserialize as shown in link above to pretty-print the xml into  clob text

 xmlserialize(document (...) indent)

where "(...)" is the xml calls above
ASKER
nssudha

It worked. Your assumptions were correct. However, when I am trying to spool to an xml file, it was giving me different CLOBs for each Hospital code. Is there a way to spool to xml as one document for all the hospital codes?

Thanks,
(Raj) Sudhakar.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sean Stuber

Using either of my queries above
I get a single clob result that has all of them in it

<MedDetails>
  <HeaderInformation>
    <DataSource>EMPR-Data</DataSource>
    <DateCreated>11/25/2013 01:03:46 PM</DateCreated>
    <EMPCount>3</EMPCount>
  </HeaderInformation>
  <MEDINFO>
    <HOSPITAL_CODE>1001</HOSPITAL_CODE>
    <M MEDICAL_CODE="Inf323">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="Inf326">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="Inf325">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="Inf324">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
  </MEDINFO>
  <MEDINFO>
    <HOSPITAL_CODE>1002</HOSPITAL_CODE>
    <M MEDICAL_CODE="SCN102">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="SCN105">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="SCN104">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="SCN103">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
  </MEDINFO>
  <MEDINFO>
    <HOSPITAL_CODE>1003</HOSPITAL_CODE>
    <M MEDICAL_CODE="VLN101">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="VLN104">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="VLN103">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
    <M MEDICAL_CODE="VLN102">
      <E Medical_Desc="NA">
        <Amount>5</Amount>
        <StDt>8/12/2013</StDt>
        <EnDt>10/31/2013</EnDt>
        <Details>NA</Details>
      </E>
    </M>
  </MEDINFO>
</MedDetails>

Open in new window

ASKER
nssudha

It is also putting the header information for each hospital code. I need one header information for the entire XML. I used "with mydata as (select hospital_code,medical_code,amount,stdt,enddt....,count(*) over() mycount
from table_name
)
select xmlserialize(document
                xmlelement("MedDetails",
                   xmlelement("HeaderInformation",
                       xmlforest('EMPR-Data' as "DataSource",
                       TO_CHAR(SYSDATE, 'mm/dd/yyyy hh:mi:ss AM') AS "DateCreated",
                         mycount AS "EMPCount"
                      )
                       ),
                      xmlelement("MEDINFO",
                       xmlelement("HOSPITAL_CODE",hospital_code),
                         xmlagg(
                             xmlelement("M",
                                xmlelement("E",
                                  xmlforest(amount AS "Amount",
                                     stdt AS "StDt",
                                     enddt AS "EnDt",
                                     details AS "Details"
                                                   )
                                                  )
                                                  )
                                                  )
                                                )
                                                 )
                           version '1.0' indent)
                             from mydata
                           group by mycount,hospital_code
/

Please let me know how to spool to a single xml document.

Thanks,
Sudhakar.
ASKER
nssudha

Never mind. I got it worked out. If I need more help, I will post again.

Thanks,
(Raj) Sudhakar.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
nssudha

Hi,

I still have one last issue with the count. I am using the above query provided to spool a file with a query similar to the one provided earlier here. I am copying the solution below. When I am running it, I am getting ORA-00937: not a single-group group function near mycount as "OrdersCount".

Could you please let me know where the issue is?

Thanks,
Sudhakar.


drop table myorders purge;
create table myorders (
RPT_ORD_ID            CHAR(32),
OrderID                  CHAR(32),
FTNT_ORD_ID            NUMBER,
OrderDesc            VARCHAR2(500),
IS_ACTV                  CHAR(1)
);

insert into myorders(orderid,orderdesc) values('1', 'Oracle Developers Guide.');
insert into myorders(orderid,orderdesc) values('2', 'Oracle DBA Guide.');
insert into myorders(orderid,orderdesc) values('3', 'Oracle PLSQL Guide.');
insert into myorders(orderid,orderdesc) values('4', 'Java Developers Guide.');
insert into myorders(orderid,orderdesc) values('5', 'Java for Dummies.');
commit;

with mydata as (
select orderid, orderdesc, count(*) over() mycount from myorders
)
select xmlserialize(document
      xmlelement("FootNoteDetails",
            xmlelement("HeaderInformation",
                  xmlforest(
                        'ORDR-Footnote' as "DataSource",
                        sysdate as "DateCreated",
                        sysdate as "DateLastUpdated",
                        mycount as "OrdersCount"
                  )
            ),
            xmlagg(
                  xmlelement("FootNote",
                        xmlforest(
                              trim(orderid) as "OrderID",
                              orderdesc as "OrderDesc"
                        )
                  )
            )
      )
version '1.0' indent)
from mydata
group by mycount
/
ASKER
nssudha

I will post my actual query in a separate post.

Thanks,
Sudhakar.