我不要以为@ M.Ali回答你的标准。他的结果集包括ID =“000”
if OBJECT_ID('Tempdb..#Work') is not null
drop table #Work;
Create Table #Work (Id char(3), [Type] int)
insert into #Work values
('456', 4)
, ('123', 4)
, ('123', 4)
, ('123', 18)
, ('123', 4)
, ('789', 4)
, ('789', 4)
, ('000', 7)
select distinct *
from #Work a
where exists (
select Type
,Count(Distinct Id) cntId
from #Work b
where a.Type = b.Type
group by Type
having Count(Distinct Id) > 1
)
and exists (
select Id
,count(distinct Type)
from #Work c
where a.Id = c.Id
group by id
having count(distinct type)= 1
)
输出:
Id Type
---- -----------
456 4
789 4
所以,你要选择那里并没有'用相同的ID,但不同类型的exist'另一行的所有行?嗯,我不知道你怎么可以这样做... –