Membangun solusi @Georges Martin, saya tidak akan menggunakan gabungan lateral dan klausa pengelompokan, melainkan hanya subquery bersarang:
SELECT XMLELEMENT(
NAME "Company",
XMLATTRIBUTES(id AS id, field1 AS field),
(SELECT XMLAGG(
XMLELEMENT(
NAME "companyTag",
XMLATTRIBUTES(
attr.key AS "tagName",
attr.value AS "tagValue"
)
)
) FROM JSONB_EACH_TEXT(attributes) AS attr)
) FROM comp_emp;