I am having a hard time figuring out how to create an XML file from a SQL select that has sub nodes to store the not unique values.
I am using Microsoft SQL Server 2012
e.g.
select company, founder
from companies
The result of the query is:
microsoft corp Bill Gates
microsoft corp Paul Allen
apple inc Steve Jobs
apple inc Ronald Wayne
apple inc Steve Wozniak
I would like to generate an XML like
<values>
<company>microsoft corp</company>
<founders>
<founder id="1">Bill Gates</founder>
<founder id="2">Paul Allen</founder>
</founders>
</values>
<values>
<company>apple inc</company>
<founders>
<founder id="1">Steve Jobs</founder>
<founder id="2">Ronald Wayne</founder>
<founder id="3">Steve Wozniak</founder>
</founders>
</company>
</values>
I am not sure the node <founders> is required, I think it would also work fine for me to have the founders directly under the <values> node, since they keep in the right company, getting an id to have a list.
What I get right now, using FOR XML, then I tried different options is the following:
<values>
<company>apple inc</company>
<founder>Steve Jobs</founder>
</values>
<values>
<company>apple inc</company>
<founder>Ronald Wayne</founder>
</values>
<values>
<company>apple inc</company>
<founder>Steve Wozniak</founder>
</values>
which is not compatible with what I need to achieve then.
Any help to get all the founders under the same <values> node is highly appreciated.
Thanks!