1

这是一个示例数据:从该表中,使用 stringvalue 检查 ID,如果 stringvalue 包含或等于 ID,它会根据满足的条件获取代码。如果至少有 1 个满足条件,则查询将连接代码的任何值并将其放在 newcode 下。

   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
4

2 回答 2

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

于 2013-02-26T03:32:54.780 回答
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

顺便说一句,您不应该convert(nvarchar, id)在 SQL Server 中使用。您应该始终提供一个长度,例如convert(nvarchar(100), id).

于 2013-02-26T02:52:21.260 回答