根据您的更新,您应该能够使用子查询来获取列表数据,然后将其连接回您的表:
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