SQL查询,没有客户端联系人和活动客户端联系人的客户端
我需要一个查询,以获取没有客户联系记录的客户的名称,如果有任何客户联系,则结束。请参见下表结构SQL查询,没有客户端联系人和活动客户端联系人的客户端,sql,sql-server,tsql,Sql,Sql Server,Tsql,我需要一个查询,以获取没有客户联系记录的客户的名称,如果有任何客户联系,则结束。请参见下表结构 Client ID Client Name ------------------------- 1 John 2 Sean 3 Johnson Client Contact ID Client ID Start Date End Date ---------------
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
表:客户
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
表:客户联系人
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
查询将返回Johnson和Sean
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
select distinct cl.*
from Client cl
left outer join Client_Contact clcnt
on cl.[Client ID] = clcnt.[Client ID]
where (clcnt.[Client ID] IS NULL OR clcnt.[End Date] IS NULL)
我希望这个问题对你有用
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
select c.* from client c
left join (select max([Client Contact ID]) ClientContactID,[Client ID] from client_contact
group by [client id]) ce on c.[Client ID]=ce.[Client ID]
left join client_contact ce1 on ce.[Client Contact ID]=ce1.[Client Contact ID]
where ce.[Client ID] is null
or ce1.[End Date] is not null
以下是我用于此类查询的查询模式,当所涉及的表变得非常大时,与“左连接”模式相比,它可以带来显著的性能提升:
Client ID Client Name
-------------------------
1 John
2 Sean
3 Johnson
Client Contact ID Client ID Start Date End Date
---------------------------------------------------------
1 1 1/1/1999 2/2/1999
2 1 1/2/1999 2/3/1999
3 1 1/3/1999 2/4/1999
4 2 1/2/2005 1/2/2007
5 2 1/3/2005 NULL
select c.[Client Name]
from Client c
where not exists
(
select *
from Client_Contact cc
where c.[Client ID] = cc.[Client ID]
and cc.[End Date] is null
)
假设您在与StartDate的客户联系中有一行,并且EndDate为空。是否应将其视为有效联系人?否如果填写了所有记录的结束日期,则客户应显示检查过的答案。看起来很酷。如果我的回答对你有效,请将其标记为最佳答案=D