C
clalc
Sorry I repost my question because I think my previous post got burried.
I have to subtract a value from one table and calculate the result in
another table. Here are tables and desired values:
Table Demand
Item Qty Date
aaa 10 03-mar-2010
aaa 7 05-mar-2010
aaa 5 12-mar-2010
ccc 8 06-mar-2010
ccc 12 09-mar-2010
Table Stock
Item Qty
aaa 12
bbb 9
ccc 6
the result should be table Net_Demand
Item Qty Date (how to get column Qty and left from
Stock table values)
aaa 0 03-mar-2010 (10-12, left 2)
aaa 5 05-mar-20 (7-2, left 0)
aaa 5 12-mar-2010 (5-0, left 0)
ccc 2 06-mar-2010 (8-6, left 0)
ccc 12 09-mar-2010 (12-0, left 0)
Here is SQL that was suggested to solve it, but it does not give all the
results as above:
SELECT Demand.Item, Demand.Date, (SELECT IIF((Stock.[Qty] -
Sum([XX].[QTY])) >0,0, (Stock.[Qty] - Sum([XX].[QTY]))*-1) FROM
Demand AS [XX] WHERE [XX].Date <= Demand.Date AND [XX].Item
= Demand.Item) AS ToOrder
FROM Demand LEFT JOIN Stock ON Demand.Item = Stock.Item
ORDER BY Demand.Item, Demand.Date;
Would you have any suggestions ? Please help.
I have to subtract a value from one table and calculate the result in
another table. Here are tables and desired values:
Table Demand
Item Qty Date
aaa 10 03-mar-2010
aaa 7 05-mar-2010
aaa 5 12-mar-2010
ccc 8 06-mar-2010
ccc 12 09-mar-2010
Table Stock
Item Qty
aaa 12
bbb 9
ccc 6
the result should be table Net_Demand
Item Qty Date (how to get column Qty and left from
Stock table values)
aaa 0 03-mar-2010 (10-12, left 2)
aaa 5 05-mar-20 (7-2, left 0)
aaa 5 12-mar-2010 (5-0, left 0)
ccc 2 06-mar-2010 (8-6, left 0)
ccc 12 09-mar-2010 (12-0, left 0)
Here is SQL that was suggested to solve it, but it does not give all the
results as above:
SELECT Demand.Item, Demand.Date, (SELECT IIF((Stock.[Qty] -
Sum([XX].[QTY])) >0,0, (Stock.[Qty] - Sum([XX].[QTY]))*-1) FROM
Demand AS [XX] WHERE [XX].Date <= Demand.Date AND [XX].Item
= Demand.Item) AS ToOrder
FROM Demand LEFT JOIN Stock ON Demand.Item = Stock.Item
ORDER BY Demand.Item, Demand.Date;
Would you have any suggestions ? Please help.