1

サンプル データは次のとおりです。このテーブルから ID が stringvalue でチェックされ、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

使用するだけ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 に答える
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 に答える