0

我有一个具有用户 ID 的表,另一个作为 AD 组的表;某些 AD 组会告诉您客户端是否是 VIP、Pilot、Remote Access 等组的成员。

当尝试创建一个视图来显示客户端是其中哪些组的成员时,我遇到了必须为每个组名称创建单独的连接的问题,因为不这样做会导致结果重复。

Table One
Client_ID   Client_Name
---------   ------------
A               ABC
D               DEF
G               GHI

Table Two:
Client_ID      AD_Group
---------      --------
A               I_GroupX
A               GroupY
D               B_GroupX
D               GroupZ
G               GroupY
G               GroupZ

Result Table:
Client_ID       Client_Name      VIP(X)     Pilot(Y)       RemoteAccess(Z)
---------       -----------      ------     --------        -----------
A                 ABC            I_GroupX     GroupY           Not Member
D                 DEF            Not Member   GroupY            GroupZ
G                 GHI            B_GroupX     Not Member        GroupZ

这可行,但必须为每个组创建一个,而不是在一个视图中创建一个:

SELECT Client_ID,
      ,Client_Name
      ,[VIP] = CASE
       WHEN A.GroupX IS NULL THEN 'Not Member'
                             ELSE A.AD_Group
       END
  FROM Table1 LEFT OUTER JOIN 
      (SELECT [Client_ID]
             ,[AD_Group]
        FROM Table2
       WHERE  [AD_Group] LIKE '%GroupX') A 
    ON Client_ID = A.Client_ID

提前致谢

4

1 回答 1

1
SELECT Client_ID, Client_Name,
[VIP(X)] = ISNULL((select AD_Group from Table2 where Client_Id = t1.Client_ID and AD_Group LIKE '%GroupX'), 'Not Member'),
[Pilot(Y)] = ISNULL((select AD_Group from Table2 where Client_Id = t1.Client_ID and AD_Group LIKE '%GroupY'), 'Not Member'),
[RemoteAccess(Z)] = ISNULL((select AD_Group from Table2 where Client_Id = t1.Client_ID and AD_Group LIKE '%GroupZ'), 'Not Member')
FROM Table1 t1
于 2013-04-29T20:47:58.787 回答