1

我有一个这样的主表

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,那么我只有一个产品依赖项。

现在,我需要检查是否有任何依赖项是否为假的产品。如果任何依赖项为假,那么我必须返回一些文本/值。

结果

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)
4

2 回答 2

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
于 2012-10-30T12:08:12.310 回答
0

这应该列出具有错误状态的直接子级。

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
于 2012-10-30T12:44:07.743 回答