473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

SQL/XML nested child problem

Hello all,

I ran into an interesting SQL/XML question yesterday. A customer wants
to make a part of the relational data available through a webservice
as XML. This seemed like the perfect opportunity to use the DB2 XML-
capabilities. The following test scenario will make things easier to
explain:

drop table test.customer;
create table test.customer
(
id integer not null,
name varchar(255) not null,
email varchar(255),
customer_type char(1),
contact_of_customer_id integer, -- points to a custom from whom this
custom is a contact
primary key(id)
);

insert into test.customer(id, name, email, customer_type,
contact_of_customer_id) values (1, 'name1', '1@1.com', 'A', null), (2,
'name2', '2@2.com', 'A', null), (3, 'name3', '2@2.com', 'B', 2), (4,
'name4', '4@4.com', 'C', 2);

The database structure allows for customers to have contacts (which in
turn are customers as well, etc). In practice, the application will
allow only one level of contacts, so no recursion is required. As you
can see, customer 1 and 2 are of the required type 'A', 1 having no
contacts, 2 having customers 3 and 4 as contact.

The XML should contain a list of customers (including their details),
and each one that has contacts should have them listed as child nodes.
This gives the following (the output format is fixed, I can't change
it):

<customers>
<customer>
<customerId>1</customerId>
<customerInfo>
<customerName>name1</customerName>
<customerEmail>1@1.com</customerEmail>
</customerInfo>
</customer>
<customer>
<customerId>2</customerId>
<customerInfo>
<customerName>name2</customerName>
<customerEmail>2@2.com</customerEmail>
</customerInfo>
<customerContact>
<customerId>3</customerId>
<customerInfo>
<customerName>name3</customerName>
<customerEmail>2@2.com</customerEmail>
</customerInfo>
</customerContact>
<customerContact>
<customerId>4</customerId>
<customerInfo>
<customerName>name4</customerName>
<customerEmail>4@4.com</customerEmail>
</customerInfo>
</customerContact>
</customer>
</customers>

I've created the following xquery that returns one record containing
the neccesary XML:

XQUERY
let $customers := db2-fn:sqlquery(
"
select
XMLELEMENT
(
NAME ""customer"",
XMLELEMENT
(
NAME ""customerId"",
cust.id
),
XMLELEMENT
(
NAME ""customerInfo"",
XMLFOREST
(
cust.name as ""customerName"",
cust.email as ""customerEmail""
)
),
XMLAGG
(
XMLELEMENT
(

NAME ""customerContact"",
XMLELEMENT
(
NAME ""customerId"",
cont.id
OPTION NULL ON NULL
),
XMLELEMENT
(
NAME ""customerInfo"",
XMLFOREST
(
cont.name as ""customerName"",
cont.email as ""customerEmail""
)
OPTION NULL ON NULL
)
OPTION NULL ON NULL
)
)
)

from test.customer cust
left outer join test.customer cont on cont.contact_of_customer_id =
cust.id
where cust.customer_type = 'A'
group by cust.id, cust.name, cust.email
")

return
<customers>{$customers}</customers>;

It selfjoins customer (cust and cont) and groups by the fields in
cust. The XMLAGG function does the rest. This was based on the example
used in the SQL Reference Vol.1: XMLELEMENT, regarding the
departments.

The query functions correctly. Notice however the GROUP BY-statement
at the end of it. The real table contains much more data, including
data from other tables as well. With this query, I have to GROUP BY on
every column I want to include in customer. This seems unnecessary and
wasteful. I am sure that there is a better way to write this one.

XQuery guru's (and even lesser gods), I would like to ask you to take
your best shot at it.

Many thanks in advance,

Frederik
Jan 9 '08 #1
0 1408

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
2
by: DelphiBlue | last post by:
I have a Nested Datagrid that is using a data relations to tie the parent child datagrids together. All is working well with the display but I am having some issues trying to sort the child...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: miked | last post by:
I am architecting in a read only class for use in mapping data to a business object. The object makes strong use of nested classes and their ability to access protected fields. The downside is...
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
1
by: masri999 | last post by:
Hello, I want to share my experiences about using insert into exec which may help others . Using SQL Server 2000, SP3 . Two Proceduers - Parent SP caliing a Child SP (nested ) . No...
0
by: Adam Knight | last post by:
Hi All, I have a repeater control nested inside another repeater control. The nested (child) repeater control, is bound to an object data source. My problem is, i need to send a data item...
0
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any...
2
by: GISmatters | last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files...
0
by: omgdork | last post by:
Hi! I'm having trouble on how to proceed with inserting data that are in parent and child gridviews. I have a gridview with nested gridviews that looks like this: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.