Возможное решение, если вы ищете только все записи дочерних строк во второй таблице, которые имеют все ложные значения Isactive
живая демонстрационная ссылка
create table tempCTE_CompMCompDir (Id int, CompanyId int, ChildId int)
insert into tempCTE_CompMCompDir
values
(24, 1776, 1781),
(24, 1776, 1782),
(25, 1777, 1783),
(24, 1781, 1786),
(24, 1781, 1787);
create table tempContComM(CompanyId int, ContactId int, IsActive bit);
insert into tempContComM
values
(1787 ,2903 , 1),
(1783 ,2903 , 0),
(1778 ,2903 , 0),
(1786 ,2903 , 0);
select t.* from tempCTE_CompMCompDir t join (
select
id,value=max(case when ISNULL(Isactive,-1)=0 then 0 else 1 end)
from tempCTE_CompMCompDir l
left join tempContComM r
on l.companyid=r.companyid or l.childid=r.companyid
group by id
) t2
on t.id=t2.id and t2.value=0
Пара примечаний:
- Я не считал недостающие значения сопоставления ни ложными, ни истинными
- вы можете настроить max (case ..), чтобы изменить свои правила и критерии