如何根据特定ID将一个列值减去另一个列值

时间:2013-12-04 17:53:59

标签: sql

我的查询

select c.Date, d.PartyName, e.ItemName, a.InwardNo, b.Quantity as [Inward Quantity],    ISNULL(c.Quantity, 0) as [Outward Quantity],
(ISNULL(b.Quantity, 0) - ISNULL(SUM(c.Quantity), 0)) as [Balance Quantity], e.Weight
from tblInwardParty a
Left Join tblInwardItems b ON a.InwardNo = b.InwardNo
Left Join tblOutwardItem c ON a.PartyCode = c.PartyCode and b.ItemCode = c.ItemCode and     a.InwardNo = c.InwardNo
Join tblPartyMaster d ON a.PartyCode = d.PartyCode
Join tblItemMaster e ON b.ItemCode = e.ItemCode
where a.InwardNo = 19778
Group By a.InwardNo, PartyName, ItemName, c.Date, Weight, b.Quantity, c.Quantity
Order By PartyName

我的输出是

--------------------------------------------------------------------------------------------------------------------
 Date             Party Name      Item Name   Inward No     Inward Qua     Outward Qua             Balance Qua       Weight
---------------------------------------------------------------------------------------    -----------------------------
06/05/13    A R ENTERPRISE  SODA ASH IMP    19778       400     17       383        50
07/10/13    A R ENTERPRISE  SODA ASH IMP    19778       400     100         300     50
10/11/13    A R ENTERPRISE  SODA ASH IMP    19778       400     1          399      50
13/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     20         380      50
22/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     200         200     50
22/07/13    A R ENTERPRISE  SODA ASH IMP    19778       400     30         370      50
27/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     20          380     50
28/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     10          390     50

所以我的询问是,当我减去400 - 17时,它给出383结果它在第一次是好的但第二次再次服用 400 - 100并给出我不想要的余额300,我只想将以前的余额作为实际余额并根据相同的Party_Name和相同的Item_Name

减去当前的余额

就像那样

---------------------------------------------------------------------------------------    -----------------------------
Date             Party Name      Item Name   Inward No     Inward Qua     Outward Qua         Balance Qua       Weight
---------------------------------------------------------------------------------------    -----------------------------
06/05/13    A R ENTERPRISE  SODA ASH IMP    19778       400     17          383     50
07/10/13    A R ENTERPRISE  SODA ASH IMP    19778       400     100         283     50
10/11/13    A R ENTERPRISE  SODA ASH IMP    19778       400     1          282      50
13/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     20         262      50
22/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     200         62      50
22/07/13    A R ENTERPRISE  SODA ASH IMP    19778       400     30          32      50
27/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     20          12      50
28/06/13    A R ENTERPRISE  SODA ASH IMP    19778       400     10        2     50
--------------------------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

而不是这样做是单个查询,它可能更容易创建存储过程。一种方法是创建一个表然后更新,或创建一个简单输出的临时表。这是让你走上正确轨道的东西......

DECLARE @Table TABLE
 (ID INT, 
  TheDate DATETIME, 
  PartyName VARCHAR(50), 
  Amount INT, 
  RunningTotal INT etc...)

DECLARE @ID INT, @Amount INT, @RunningTotal INT

INSERT INTO @Table
SELECT....  --here you can insert all the fields except for the RunningTotal

DECLARE #cTmp CURSOR LOCAL FAST_FORWARD FOR 
SELECT ID, Amount FROM @Table

OPEN #cTmp
FETCH NEXT FROM #cTmp INTO @ID, @Amount

WHILE @@FETCH_STATUS = 0
BEGIN
  --Process each row
  SET @RunningTotal = @RunningTotal + @Amount

  --Update the table
  UPDATE @Table SET RunningTotal = @RunningTotal WHERE ID=@ID

  FETCH NEXT FROM #cTmp INTO @ID, @Amount
END
CLOSE #cTmp
DEALLOCATE #cTmp

SELECT * FROM @Table