SQL连接以识别组成员

时间:2019-07-20 06:41:47

标签: sql sas self-join

我有一个类似于以下内容的客户表:

    Client List 

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

我需要将其映射到Customer_Dim表

    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. 从下表中获取他们的组号(我可以通过简单的左联接来做到)
  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

请让我知道是否需要其他信息。

很抱歉,如果之前曾问过类似的问题-进行了几次搜索,但找不到任何东西。

谢谢

4 个答案:

答案 0 :(得分:1)

加入表以获取客户端列表中所有客户端的组号,然后从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               

答案 1 :(得分:0)

我认为从Group No.表中获取已发布的结果非常简单。

如果您不希望使用 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.] ) 的ClientList

Group No.

如果您只需要 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.] ) 的ClientList

{{1}}

答案 2 :(得分:0)

要获得必需的结果,您需要在加入条件

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'

答案 3 :(得分:0)

对于客户端处于非活动状态并想要在结果集中标识客户端的情况,您可以使用GROUP BY进行LEFT JOIN,并利用HAVING子句中的Proc SQL自动合并作为选择条件。

  inmailList.forEach((inmail) => {
        Object.keys(inmail).forEach((key) => {
            if (inmail[key] === 'message') 
        })
    })