我不要以爲@ 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'另一行的所有行?嗯,我不知道你怎麼可以這樣做... –