这是一个示例数据:如果stringvalue包含或等于ID,则使用stringvalue检查此表ID,它根据满足的条件获取代码。如果存在至少1个满足条件,则查询将连接代码的任何值并将其置于新代码下。
Scenario 1
ID CODE VALUE Newcode
4 42 44,45
4 43 44,45
4 44 2,4,6 44,45
4 45 2,4,6 44,45
Scenario 2
ID CODE VALUE Newcode
4 42 44
4 43 44
4 44 4 44
4 45 3 44
答案 0 :(得分:0)
只需使用list_agg
:
select sub.mainid, list_agg(cast(sub_code as nvarchar(100)), ',')
from ENPTREEATTRS SUB
WHERE SUB.CODE in ('42','43','44','45') and
SUB.STRINGVALUE LIKE '%'||cast(id as nvarchar(100))||'%'
group by sub.mainid
顺便说一下,你不应该在SQL Server中使用convert(nvarchar, id)
。您应该始终提供长度,例如convert(nvarchar(100), id)
。
答案 1 :(得分:0)
根据您的更新,您应该能够使用子查询来获取列表数据,然后将其加入到您的表中:
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
击> <击> 撞击>