我有一个像这样的主表
Product Status Product id
A True 1
B True 2
C true 3
D True 4
E false 5
F True 6
现在我有其他表格给了我产品的依赖性
Product DependencyId
C 2
C 5
E 1
B 4
B 6
现在,当我搜索C时,我可以看到两种产品依赖于C,现在这两种产品依赖于其他产品。
让我说我正在寻找E,然后我只有一个产品的依赖。
现在,我需要检查产品是否有任何依赖关系是假的。如果任何依赖项为false,那么我必须返回一些text / value。
结果
When product is C then output will be E (because the grand children of C (i.e. E) has false)
When product is B then output will be NULL (becuase none of the child of B or their sub childrens has false)
答案 0 :(得分:1)
Declare @cnt int
Select *
into #tmp
from mastera where Product_ID in (Select DependencyID from dbo.dependency where Product='C')
select @cnt=0
while @cnt<>(Select count(*) from #tmp)
begin
Select @cnt=count(*) from #tmp
insert into #tmp
Select m.*
from mastera m
Left join #tmp on m.Product=#tmp.Product
where m.Product_ID in (Select DependencyID from dbo.dependency where Product in (Select Product from #tmp where status=1))
and #tmp.Product is null
end
Select * from #tmp where Status=0
Drop table #tmp
答案 1 :(得分:0)
这应该列出具有 false 状态的直接孩子。
SELECT
C.Product
FROM
MasterTable A
JOIN
Dependency D ON D.Product = A.Product
JOIN
MasterTable C ON C.ProductId = D.DependencyId AND C.Status = 0