更新に基づいて、サブクエリを使用してリスト データを取得し、それをテーブルに結合できるはずです。
SELECT DISTINCT
ID,
CODE,
ATTRSTRINGVALUE,
SUB.Newcode
FROM Table1 A
INNER JOIN tabel2 b
ON a.MainID = b.mainID
LEFT JOIN
(
SELECT SUB.MainID,
list_agg(cast(SUB.CODE as nvarchar2(50)), ',') as Newcode
FROM ENPTREEATTRS SUB
WHERE SUB.CODE in ('42','43','44','45')
AND SUB.STRINGVALUE LIKE '%'||cast(ID as nvarchar2(50))||'%'
GROUP BY SUB.MainID
) SUB
ON SUB.MainID = A.MainID
使用できる投稿した新しいクエリの後に#1を編集します。
SELECT DISTINCT
ID,
CODE,
ATTRSTRINGVALUE,
SUB.Newcode
FROM ENPBATREE A
LEFT OUTER JOIN enptreeattrs B on A.MainID = B.MainID
LEFT OUTER JOIN ENPTREEATTRS B1 on B.MainID = B1.MainID
LEFT OUTER JOIN ENPTREEATTRS B2 on B1.MainID = B2.MainID
LEFT OUTER JOIN ENPENTRYACCESSUSERSROLES D on B2.MainID = D.MainID
LEFT OUTER JOIN ENPENTRYACCESSLIST F on D.USERNAME = F.USERNAME
LEFT JOIN
(
SELECT SUB.MainID,
list_agg(cast(SUB.CODE as nvarchar2(50)), ',') as Newcode
FROM ENPTREEATTRS SUB
WHERE SUB.CODE in ('42','43','44','45')
AND SUB.STRINGVALUE LIKE '%'||cast(ID as nvarchar2(50))||'%'
GROUP BY SUB.MainID
) SUB
ON SUB.MainID = A.MainID
サブクエリなしでこれを実行できる場合があります。
SELECT
A.TREEENTRYID,
F.accessid,
b1.ATTRID,
b1.attrstringvalue,
list_agg(cast(SUB.ATTRID as nvarchar2(50)), ',') as Newcode
FROM ENPBATREE A
LEFT OUTER JOIN enptreeattrs B on A.TREEENTRYID = B.TREEENTRYID
LEFT OUTER JOIN ENPTREEATTRS B1 on B.TREEENTRYID = B1.TREEENTRYID
LEFT OUTER JOIN ENPTREEATTRS B2 on B1.TREEENTRYID = B2.TREEENTRYID
LEFT OUTER JOIN ENPENTRYACCESSUSERSROLES D on B2.TREEENTRYID = D.TREEENTRYID
LEFT OUTER JOIN ENPENTRYACCESSLIST F on D.USERNAME = F.USERNAME
LEFT JOIN enptreeattrs SUB
ON SUB.MainID = A.MainID
WHERE SUB.ATTRID in ('42','43','44','45')
AND SUB.ATTRSTRINGVALUE LIKE '%'||cast(F.ACCESSID as nvarchar2(50))||'%'
GROUP BY A.TREEENTRYID, F.ACCESSID,
b1.ATTRID, b1.attrstringvalue;
SELECT
ID,
CODE,
ATTRSTRINGVALUE,
list_agg(cast(SUB.CODE as nvarchar2(50)), ',') as Newcode
FROM ENPBATREE A
LEFT OUTER JOIN enptreeattrs B on A.MainID = B.MainID
LEFT OUTER JOIN ENPTREEATTRS B1 on B.MainID = B1.MainID
LEFT OUTER JOIN ENPTREEATTRS B2 on B1.MainID = B2.MainID
LEFT OUTER JOIN ENPENTRYACCESSUSERSROLES D on B2.MainID = D.MainID
LEFT OUTER JOIN ENPENTRYACCESSLIST F on D.USERNAME = F.USERNAME
LEFT JOIN ENPTREEATTRS SUB
ON SUB.MainID = A.MainID
WHERE SUB.CODE in ('42','43','44','45')
AND SUB.STRINGVALUE LIKE '%'||cast(ID as nvarchar2(50))||'%'
GROUP BY ID, CODE, ATTRSTRINGVALUE