我有 3 个表-DimWarehouses
和. 我在 SQL Query 中做了一个集合。一切都会好起来的,但我需要这个结果是成本或库存不等于 0 的地方。在我的查询中,WHERE 子句不起作用。DimProducts
DimTime
如何编写可以工作的 Where 子句?
where bb.COST <>0 or bb.stock <> 0
询问:
select
bb.TimeKey,
bb.ProductKey,
bb.WarehouseKey,
bb.stock,
bb.COST
from(
SELECT dt.timekey,
a.productkey,
b.warehousekey,
Isnull((SELECT SUM(quantyti)
FROM aa f
WHERE a.productkey = f.productkey
AND b.warehousekey = f.warehousekey
AND f.timekey <= dt.timekey), 0) AS stock,
Isnull((SELECT SUM(COST)
FROM aa f
WHERE a.productkey = f.productkey
AND b.warehousekey = f.warehousekey
AND f.timekey <= dt.timekey), 0) AS COST
FROM dimproducts a,
(SELECT warehousekey FROM dimwarehouses) b,
(SELECT 20121002 AS timekey ) dt
)bb
where
bb.COST <>0 or
bb.stock <> 0
表 aa:
TimeKey ProductKey WarehouseKey Quantyti Cost
20120901 1 1 10.000 100.00
20120901 1 1 -10.000 -100.00
20120901 2 1 5.000 50.00
20120901 2 1 15.000 150.00
20120901 2 2 3.000 30.00
20120901 3 1 0.000 20.00
20120901 3 2 10.000 0.00
结果:
TimeKey ProductKey WarehouseKey stock COST
20121002 2 1 20.000 200.00
20121002 3 1 0.000 20.00
20121002 2 2 3.000 30.00
20121002 3 2 10.000 0.00
我需要结果:
TimeKey ProductKey WarehouseKey stock COST
20121002 2 1 20.000 200.00
20121002 2 2 3.000 30.00