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
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
ASKER
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.
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.
>>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?
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?
ASKER
Yes. I want E nodes to be the children of MGR. I might have made a mistake while masking the XML.
Thanks,
Sudhakar.
Thanks,
Sudhakar.
I might have made a mistake while masking the XML.
if so, can you post a corrected XML?
if so, can you post a corrected XML?
ASKER
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.
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.
ASKER
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
Thanks,
Sudhakar.
QualityData2.xml
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:
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
ASKER
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.
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.
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.
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
Sure. I will work on it and post it soon.
Thanks,
Sudhakar.
Thanks,
Sudhakar.
ASKER
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
Hope these are better samples.
Thanks,
Sudhakar.
MyData.xml
MedicalData.xls
ASKER
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>
Please let me know if you have a way to get the count also.
Thanks,
Sudhakar.
<link removed - GaryC123>
I'll try to get some time later today to work on this.
ASKER
Thank you slightwv.
Thanks,
(Raj) Sudhakar.
Thanks,
(Raj) Sudhakar.
>>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/questions/28269030/Oracle-XML-Help.html?anchorAnswerId=39580824#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?
I showed you this in your previous question:
https://www.experts-exchange.com/questions/28269030/Oracle-XML-Help.html?anchorAnswerId=39580824#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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
xmlserialize(document (...) indent)
where "(...)" is the xml calls above
ASKER
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.
Thanks,
(Raj) Sudhakar.
Using either of my queries above
I get a single clob result that has all of them in it
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>
ASKER
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,st dt,enddt.. ..,count(* ) over() mycount
from table_name
)
select xmlserialize(document
xmlelement("MedDetails",
xmlelement("HeaderInformat ion",
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.
from table_name
)
select xmlserialize(document
xmlelement("MedDetails",
xmlelement("HeaderInformat
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"
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
Never mind. I got it worked out. If I need more help, I will post again.
Thanks,
(Raj) Sudhakar.
Thanks,
(Raj) Sudhakar.
ASKER
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("FootNoteDetail s",
xmlelement("HeaderInformat ion",
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
/
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
insert into myorders(orderid,orderdesc
insert into myorders(orderid,orderdesc
insert into myorders(orderid,orderdesc
insert into myorders(orderid,orderdesc
commit;
with mydata as (
select orderid, orderdesc, count(*) over() mycount from myorders
)
select xmlserialize(document
xmlelement("FootNoteDetail
xmlelement("HeaderInformat
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
I will post my actual query in a separate post.
Thanks,
Sudhakar.
Thanks,
Sudhakar.
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?