SQL Server:不重复时返回结果
我似乎无法集中注意力在这件事上。为了尽可能简化,假设我有一个表:SQL Server:不重复时返回结果,sql,sql-server,logic,Sql,Sql Server,Logic,我似乎无法集中注意力在这件事上。为了尽可能简化,假设我有一个表: Id cid Account 1 4010 Bank Co 2 5323 Webazon 3 3513 Internal 4 3513 PhoneCo 5 5597 Internal 我想返回所有结果,除了Account='Internal'行,其中还有一个客户具有相同的ci
Id cid Account
1 4010 Bank Co
2 5323 Webazon
3 3513 Internal
4 3513 PhoneCo
5 5597 Internal
我想返回所有结果,除了Account='Internal'行,其中还有一个客户具有相同的cid。在本例中,我们将返回第1、2、4和5行。第3行不会返回,因为“PhoneCo”和“Internal”共享cid 3513。但是,第5行将被返回,因为没有其他记录共享cid 5597
我正在与工会合作,第一部分是消除所有“内部”记录,第二部分是我感兴趣的记录,但我可能走错了方向。这里有一种方法:
select t.*
from t
where t.account <> 'Internal' or
not exists (select 1
from t t2
where t2.cid = t.cid and t2.account <> 'Internal'
);
也就是说,选择所有非内部记录。并且,为没有相应非内部账户的内部账户选择记录。这里有一种方法:
select t.*
from t
where t.account <> 'Internal' or
not exists (select 1
from t t2
where t2.cid = t.cid and t2.account <> 'Internal'
);
也就是说,选择所有非内部记录。并且,为没有相应非内部账户的内部账户选择记录。看起来很有效。我只是有时候很难在SQL世界中找到自己的想法。谢谢这看起来很有效。我只是有时候很难在SQL世界中找到自己的想法。谢谢