I have several many to many relationships in my schema. For example, a package has many taskgroups, taskgroups in turn have many tasks. All tables are linked together via many to many tables, holding for instance the primary key of package and the primary key of taskgroup. (I know this is not strictly needed, since XML is one to many, but I couldn't think of a better structure).
Is it possible to get the query result as XML, reflecting the one-to-many structure? So, the result should be like this:
<package id=1>
<taskgroup id=1>
<task id=1>
<task id=2>
</taskgroup>
<taskgroup id=2>
<task id=3>
</taskgroup>
</package>
I have managed to get part of what I want by using the XMLELEMENT() and XMLATTRIBUTE() functions to get all the tasks. Like so:
SELECT XMLELEMENT(name task,
XMLATTRIBUTES(p.name as packageName),
XMLELEMENT(name description, t.description),
XMLELEMENT(name tutorial,
XMLELEMENT(name someTaskChild1, t.data1)),
XMLELEMENT(name objectives,
XMLELEMENT(name someTaskChild2, t.data2)),
)
FROM packages p
INNER JOIN package_taskgroup pt ON p.id = pt.package_id
INNER JOIN taskgroups tg on pt.taskgroup_id = tg.id
INNER JOIN taskgroup_task tt on tg.id = tt.taskgroup_id
INNER JOIN tasks t on tt.task_id = t.id
WHERE p.id = somePackageId AND tg.id = someTaskGroupId
The question is how to group these tasks into their parent table elements?