xml - Postgres 到 xml 排序依据
问题描述
我们正在执行以下 postgres 查询:
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
如何使用 Postgres xml 函数来聚合不同 XML 级别的数据,即第一级 AT.ID+AT.goal 和第二级 SS.ID + SS.goal?
编辑
我们设法获得了我们想要的信息,但仍有 1 个问题......使用的 SQL 如下:
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
但是我们无法通过任务索引TK.index获取订单。我们必须确保它们的顺序正确,到目前为止我们还无法做到……
解决方案
完成,设法做到这一点...
我会贴出来供参考。
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
再次感谢你
推荐阅读
- javascript - 如何解决来自 eslint 的这个错误?
- html - 为什么我的 CSS 格式没有出现在 Pandoc HTML 文档中?
- django - 总结 Django 中多个模型的总量
- python - 使用列表中的每个值更新字典键
- time-complexity - 证明 f(n) 是 O(n^4)
- c# - Azure 函数设计指南
- makefile - 如何使用makefile检查是否存在具有特定扩展名的文件
- python - 将函数传递给 odeint,该函数应根据 odeint 解决方案中的每个时间步而减少
- java - 如何找到两个字符串之间不同的单词?
- node.js - CosmosDB + NestJS:系统中已经存在指定id的实体