SQL 加入以识别群组成员

SQL join to identify group members

我有一个客户 table,大致如下所示:

    Client List 

    customer no.    Customer name
    123            Kristen Smith
    128            Jeremy Church
    127            Alan Li
    132            Ryan Nelson

我需要将它映射到 Customer_Dim table

    Customer_Dim

    customer no.    Customer name   Group no.   Group Name          Cust_Active Flag
    123             Kristen Smith   5491        Zealong Tea Estate  Y
    167             Anna Hathaway   5823        AA Insurance        Y
    146             Simon Joe       5671        Direct Automobile   Y
    148             Henry Wilson    5823        AA Insurance        Y
    195             Graham Brown    5491        Zealong Tea Estate  Y
    172             Daria Smith     5671        Direct Automobile   N
    122             Dyana Smith     5823        AA Insurance        N
    132             Ryan Nelson     5671        Direct Automobile   N
    128             Jeremy Church   5823        AA Insurance        Y
    127             Alan Li         5671        Direct Automobile   Y
  1. 从 table 下面获取他们的组号(我可以通过简单的左连接来完成)
  2. 从客户客户的组号中列出所有剩余的客户(活跃的)[我无法执行此第二部分]:

要求的结果:

    Customer No.    Customer name   Group No.       Group Name
    123             Kristen Smith   5491          Zealong Tea Estate
    128             Jeremy Church   5823          AA Insurance
    127             Alan Li         5671          Direct Automobile
    195             Graham Brown    5491          Zealong Tea Estate
    167             Anna Hathaway   5823          AA Insurance
    148             Henry Wilson    5823          AA Insurance
    146             Simon Joe       5671          Direct Automobile

如果需要任何其他信息,请告诉我。

抱歉,如果之前有人问过类似的问题 - 进行了多次搜索但找不到任何内容。

谢谢

我认为从 Customer_Dim table.

获取您发布的结果非常简单

如果您不想要 Group No. 的 ClientList

 select * from Customer_Dim 
 where [Cust_Active Flag] = 'Y' 
 and [Group No.] not in (
 select CD.[Group No.] from [Client List] as CL inner join Customer_Dim as CD where CL.[customer no.] = CD.[customer no.] )

如果您只想要 Group No. 个 ClientList

 select * from Customer_Dim 
 where [Cust_Active Flag] = 'Y' 
 and [Group No.] in (
 select CD.[Group No.] from [Client List] as CL inner join Customer_Dim as CD  where CL.[customer no.] = CD.[customer no.] )

要获得所需的结果,您需要在加入中加入一个条件

SELECT *
 FROM Client            c
 JOIN Customer_Dim      cd       on c.CustomerNo = cd.CustomerNo 
                                and cd.Cust_ActiveFlag ='Y'

SELECT *
 FROM Client            c
 JOIN Customer_Dim      cd       on c.CustomerNo = cd.CustomerNo 
 WHERE cd.Cust_ActiveFlag ='Y'

加入表格以获取客户端列表中客户端的所有组号,然后select从customer_dim中仅获得这些组号中处于活动状态的客户端:

select * from customer_dim
where 
  cust_active_flag = 'Y'
  and 
  groupno in (
    select groupno
    from client_list l inner join customer_dim d
    on d.customerno = l.customerno
  )

参见demo
结果:

> customerno | customername  | groupno | groupname          | cust_active_flag
> ---------: | :------------ | ------: | :----------------- | :---------------
>        123 | Kristen Smith |    5491 | Zealong Tea Estate | Y               
>        167 | Anna Hathaway |    5823 | AA Insurance       | Y               
>        146 | Simon Joe     |    5671 | Direct Automobile  | Y               
>        148 | Henry Wilson  |    5823 | AA Insurance       | Y               
>        195 | Graham Brown  |    5491 | Zealong Tea Estate | Y               
>        128 | Jeremy Church |    5823 | AA Insurance       | Y               
>        127 | Alan Li       |    5671 | Direct Automobile  | Y               

对于客户端处于非活动状态并希望在结果集中识别客户端的情况,您可以使用 GROUP BY 执行 LEFT JOIN 并利用 Proc SQL 在 HAVING 子句中自动重新合并选择标准.

data client_list; input 
custno custname:& .; datalines;
  123   Kristen Smith
  128   Jeremy Church
  127   Alan Li
  132   Ryan Nelson
  899   Julius Caesar
run;

data customer_dim; input
custno custname:& .  groupnum groupname:& .    Cust_Active_Flag: .; datalines;
  123   Kristen Smith   5491     Zealong Tea Estate  Y
  167   Anna Hathaway   5823     AA Insurance        Y
  146   Simon Joe       5671     Direct Automobile   Y
  148   Henry Wilson    5823     AA Insurance        Y
  195   Graham Brown    5491     Zealong Tea Estate  Y
  172   Daria Smith     5671     Direct Automobile   N
  122   Dyana Smith     5823     AA Insurance        N
  132   Ryan Nelson     5671     Direct Automobile   N
  128   Jeremy Church   5823     AA Insurance        Y
  127   Alan Li         5671     Direct Automobile   Y
  231   Donald Duck     7434     Orange Insurance    Y 
  899   Julius Caesar   4999     Emperors            N
  900   Joshua Norton   4999     Emperors            N
  925   Joaquin Guzman  4999     Emperors            Y
  925   Naruhito        4999     Emperors            Y
  run;

  proc sql;
    create table want(label="Active customers of clients groups") as
    select 
      LIST.custno as client,
      DIM.*
    from 
      customer_dim DIM
    left join
      client_list LIST
    on 
      DIM.custno = LIST.custno
    group by
      groupnum
    having 
      N(LIST.custno) > 0
      and 
      (
        cust_active_flag = 'Y'
        or LIST.custno is not NULL
      )
    order by
      groupnum, custno
    ;