我目前正在尝试获取 SQL 表中给定证券的交付总量。如果交易类型为正 (+) [txn 类型],我想计算每一行并将它们相加,或者如果交易类型为负 (-),则从其余结果中删除该总数。我目前可以使用以下代码返回所有行的总和
SELECT [STOCK REC NO],
SUM(ISNULL([QTY DELIVERED], 0) + ISNULL([QTY DUE IN], 0) - ISNULL([QTY DUE OUT], 0)) AS [TOTAL QUANTITY]
FROM bla_bla
WHERE ([EVENT DATE] < '09/26/2012')
GROUP BY [STOCK REC NO]
TXN TYPE QTY DELIVERED QTY DUE IN QTY DUE OUT EVENT DATE
+ 1 28/11/2005
+ 2 07/02/2006
- 3 22/11/2006
+ 20 18/04/2011
+ 40 19/04/2011