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を検索すると、2つの製品がCに依存していることがわかります。これらの2つの製品は、他の製品に依存しています。

そして、私がEを検索しているとすると、製品の依存関係は1つだけです。

ここで、依存関係が偽であるかどうかを確認する必要があります。依存関係のいずれかがfalseの場合、テキスト/値を返す必要があります。

結果

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

これにより、ステータスが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
于 2012-10-30T12:44:07.743 に答える