[SQL] 查询出不在特定批量记录中的数据

create table #Account(AccountId int)

insert into #Account
select 1500722090
union select 1500722091
union select 1500722092
union select 1500722316
union select 1500722317
--'union' is auto filter duplication data
--'union all' is get all data
select a.AccountId, sum(case when b.AccountId is not null then 1 else 0 end) as flag from #Account a left join dbo.AccountHolding b on a.AccountId=b.AccountId group by a.AccountId having sum(case when b.AccountId is not null then 1 else 0 end)=0

 

posted @ 2017-07-06 10:28  Sunshine168  阅读(357)  评论(0)    收藏  举报