这是一个令人头疼的问题(对我而言)......
我有两张表A和B. A包含产品说明,B包含产品代码,数量和帐户。这里的皱纹是B在产品代码上有父子关系。
我想要做的是将结果集放在一起,我看到Account,Product,Description和Qty,其中一个产品数量= 0,除非产品说明中有关键字(其数量为I)不关心)。
所以,现在是我的查询:
SELECT Bp.Account, Bp.Product, A.Description, Bp.Qty FROM B
AS Bp JOIN A as Prod ON (Bp.Product = Prod.Product)
WHERE EXISTS (Select * from B AS Bc
WHERE Bp.Account = Bc.Account
AND Qty = 0)
这会返回一个非常好的结果集:
Account Product Description Qty
ABC XYZ DESC-BLAH 1
ABC XYY DESC-DEE 0
ABC ZXY DESC-BILP 1
BDE XAZ DESC-OOPS 1
BDE XYY DESC-DEE 0
BDE ZXY DESC-BLIP 1
所以我想说我想从我的结果集中排除DESC-OOPS以及与之相关的所有产品。我想看看:
Account Product Description Qty
ABC XYZ DESC-BLAH 1
ABC XYY DESC-DEE 0
ABC ZXY DESC-BILP 1
这可以在一个查询中完成吗?
非常感谢提前!
答案 0 :(得分:1)
也许你可以添加到你的WHERE标准:
SELECT Bp.Account, Bp.Product, A.Description, Bp.Qty FROM B
AS Bp JOIN A as Prod ON (Bp.Product = Prod.Product)
WHERE EXISTS (Select * from B AS Bc
WHERE Bp.Account = Bc.Account
AND Qty = 0)
AND Bp.ACCOUNT NOT IN (SELECT ACCOUNT FROM Table WHERE Description = 'DESC-OOPS'
我不确定为什么这对你不起作用,但也许:
SELECT *
FROM (SELECT Bp.Account, Bp.Product, A.Description, Bp.Qty
FROM B AS Bp
JOIN A as Prod
ON (Bp.Product = Prod.Product)
WHERE EXISTS (Select * from B AS Bc
WHERE Bp.Account = Bc.Account
AND Qty = 0)
)sub
WHERE ACCOUNT NOT IN (SELECT ACCOUNT FROM Table WHERE Description = 'DESC-OOPS'