1

We are executing the following postgres query:

select
AT.id,
AT.goal,
SS.id,
SS.index,
SS.goal,
TK.id,
TK.index,
TK.label,
TK.actions,
TK.type,
TK.group,
TK.blind,
TK.dynamic
from
automations as SS,
automation_sets as AT,
tasks as TK
where
SS.automation_set = 321
AND SS.automation_set = AT.id
AND TK.automation = SS.id
order by
SS.index, TK.index

which returns the following data:
data extraction

How can use Postgres xml functions to aggregate the data in different XML levels, being the first level AT.ID+AT.goal and the second level SS.ID + SS.goal?


EDIT
We managed to get info we want but still have 1 issue... The SQL used is the following:

SELECT XMLELEMENT(name automation_set,
    XMLATTRIBUTES(ASET_ID as id),
        XMLAGG(xml_task_group))
FROM (SELECT AT.id as ASET_ID,
        XMLELEMENT(name automation,
        XMLATTRIBUTES(SS.goal as id),
            XMLAGG(XMLELEMENT(name task,
            XMLATTRIBUTES(TK.id as task_id, TK.label, TK.actions, TK.type, TK.group, TK.blind, TK.dynamic, TK.index)))) as xml_task_group
from
    automations as SS,
    automation_sets as AT,
    tasks as TK
where
    SS.automation_set = 321
    AND SS.automation_set = AT.id
    AND TK.automation = SS.id
group by 
      AT.id, SS.id
order by 
      SS.index) t
group by t.ASET_ID

but we are unable to get the order by TK.index, which is the task Index. We must make sure that they are in right order and we are unable to do it so far...

3
  • Can you post an example of the XML file you expect? Commented Feb 11, 2020 at 20:04
  • Hi Jim, just post an update from my side and with the kind of data I need to extract Commented Feb 12, 2020 at 11:16
  • Last request posted. Thank you in advance Commented Feb 12, 2020 at 14:43

1 Answer 1

1

Done, manage to do it...

I'll posted it for reference.

SELECT XMLELEMENT(name automation_set,
    XMLATTRIBUTES(ASET_ID as id),
        XMLAGG(xml_task_group))
FROM (SELECT AT.id as ASET_ID,
        XMLELEMENT(name automation,
        XMLATTRIBUTES(SS.goal as id),
            XMLAGG(XMLELEMENT(name task,
            XMLATTRIBUTES(TK.id as task_id, TK.label, TK.actions, TK.type, TK.group, TK.blind, TK.dynamic, TK.index)) order by TK.index)) as xml_task_group
from
    automations as SS,
    automation_sets as AT,
    tasks as TK
where
    SS.automation_set = 321
    AND SS.automation_set = AT.id
    AND TK.automation = SS.id
group by 
      AT.id, SS.id
order by 
      SS.index) t
group by t.ASET_ID

Thank you again

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.