@Georges Martinのソリューションに基づいて、横方向の結合とグループ化句を使用せず、ネストされたサブクエリのみを使用します。
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;