计算SQL表中项目的剩余余额

时间:2017-10-05 08:33:41

标签: sql sqlanywhere

如果我们有库存所需的一切,我已经创建了一个需要交付给客户的订单表。这是表格当前的样子的一个例子:

OrderID     CustomerID          ArtID           Balance ToDeliver   Pickdate    Deliverydate
119829      Customer A          ARTICLE1        2       2           17/10/02    17/10/03
119829      Customer A          ARTICLE1        2       8           17/10/02    17/10/03
119833      Customer B          ARTICLE2        22      20          17/10/03    17/10/04
119347      Customer C          ARTICLE3        20      10          17/10/05    17/10/06
119475      Customer D          ARTICLE4        7       2           17/10/05    17/10/06
119476      Customer B          ARTICLE1        2       5           17/10/05    17/10/06

当前插入声明:

CREATE LOCAL TEMPORARY TABLE #DeliveryTable ( OrderID varchar(10),ArtID VARCHAR (16),ToDeliver numeric (16,6),Pickdate varchar (15), Deliverydate varchar(15), Balance numeric(16,6), CustomerID varchar(50))

insert into #DeliveryTable (OrderID, ArtID, ToDeliver, Pickdate, Deliverydate, Balance, CustomerID)
SELECT KORDERRAD.ko_nr AS 'OrderID', 
KORDERRAD.kor_Artnr AS 'ArtID', 
KORDERRAD.kor_rant AS 'ToDeliver', 
CONVERT(varchar(15), KORDERRAD.kor_ldat -1,11) AS 'Pickdate',
CONVERT(varchar(15), KORDERRAD.kor_ldat,11) AS 'Deliverydate',
ARTIKEL_LPLATS.lp_Saldo AS 'Balance', 
CustomerID.k_namn AS 'CustomerID',
FROM monitor.ARTIKEL ARTIKEL, monitor.ARTIKEL_LPLATS ARTIKEL_LPLATS, monitor.KORDER KORDER, monitor.KORDERRAD KORDERRAD, monitor.Kund Kund
WHERE KORDERRAD.kor_Artnr = ARTIKEL.art_Artnr 
AND ARTIKEL.Artnr = ARTIKEL_LPLATS.Artnr 
AND KORDER.ko_id = KORDERRAD.ko_id 
AND KORDER.k_id = CustomerID.k_id 
AND ((KORDERRAD.kor_rant>0) 
AND (KORDER.ko_status>0 And KORDER.ko_status Between 1 And 5) 
AND (KORDERRAD.kor_otyp In (0,1)) 
AND (KORDERRAD.kor_rtyp=1) 
AND (KORDERRAD.kor_ldat Between DATEADD(day, -14, getdate()) And DATEADD(day, 3, getdate()))
ORDER BY KORDERRAD.kor_ldat

当前查询:

SELECT 
     OrderID,
     CustomerID, 
     ArtID,
     CAST(Balance AS INT),
     CAST(ToDeliver AS INT),
     Pickdate,
     Deliverydate
FROM 
     #DeliveryTable
GROUP BY 
     Deliverydate,
     Pickdate,
     CustomerID,
     OrderID,
     ArtID,
     ToDeliver,
     Balance
ORDER BY 
     Deliverydate

我需要做的是,每次列出文章时,余额应减少该行的交付金额,以便具有相同文章的下一行将具有要检查的更新余额。

所以在上面的例子中,结果会像。

OrderID     CustomerID          ArtID           Balance ToDeliver   Pickdate    Deliverydate
119829      Customer A          ARTICLE1        2       2           17/10/02    17/10/03
119829      Customer A          ARTICLE1        0       8           17/10/02    17/10/03
119833      Customer B          ARTICLE2        22      20          17/10/03    17/10/04
119347      Customer C          ARTICLE3        20      10          17/10/05    17/10/06
119475      Customer D          ARTICLE4        7       2           17/10/05    17/10/06
119476      Customer B          ARTICLE1        -8      5           17/10/05    17/10/06

有一种使用SQL实现此目的的聪明方法吗?我们的数据库在SQL Anywhere服务器上运行。

0 个答案:

没有答案