SQL连接以标识组成员
我有一个客户机表,大致如下所示:SQL连接以标识组成员,sql,sas,self-join,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 no. Cust
Client List
customer no. Customer name
123 Kristen Smith
128 Jeremy Church
127 Alan Li
132 Ryan Nelson
我需要把它映射到一个客户表
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
从下表中获取他们的组号,我可以通过简单的左连接来实现
要从客户的组号中列出所有剩余的活跃客户[我无法完成第二部分]:
所需结果:
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\u Dim表中获取发布结果非常简单 如果您不想要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.] )
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的组号
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.] )
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'
或
加入表以获取客户端列表中客户端的所有组号,然后从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
)
看。
结果:
如果客户机处于非活动状态,并且希望在结果集中标识客户机,则可以使用GROUP BY进行左连接,并利用HAVING子句中的Proc SQL自动重新合并作为选择条件
data client_list; input
custno custname:& $30.; datalines;
123 Kristen Smith
128 Jeremy Church
127 Alan Li
132 Ryan Nelson
899 Julius Caesar
run;
data customer_dim; input
custno custname:& $30. groupnum groupname:& $30. Cust_Active_Flag: $1.; 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
;
请发布您的查询到目前为止您尝试了什么?客户列表中的客户是否会处于非活动状态?是,客户列表中有相当多的非活动客户,因为干系人有一个过时的列表。@Richard,这是否意味着我们可能必须在第二个select语句中添加另一个where子句,以不包括组号。使用“N”活动标记如果您希望原始非活动客户在结果集中,您需要有一个允许其custno将处于非活动状态-仅使用flag='Y'条件的查询将消除它们。我的查询基于您发布的结果,请更新您的结果并输入相同条件下的数据。谢谢,是的,您编辑的答案确实有意义。第二个对我有用。Thanks@SachiSinha . . . 作为公认的答案,这毫无意义。客户端列表没有组号列。@SachiSinha:您可以通过向上箭头将多个AN标记为有用,但只能将一个答案标记为已接受。谢谢,现在它有意义了。在一段时间后执行SQL,因此不认为可以这样做。