J
Jackson via AccessMonster.com
Hi,
I'm trying to use a subquery to do a running sum of Qty for a query. Here is
sample data:
OrderID FillID TradeDate Account Security BS Qty Price
2 132 23-Mar-09 A ERM9 S 17 102
2 133 23-Mar-09 A ERM9 S 36 105
2 134 23-Mar-09 A ERM9 S 12 108
2 135 23-Mar-09 B ERM9 S 10 104
2 136 23-Mar-09 B ERM9 S 23 103
2 137 23-Mar-09 B ERM9 S 7 106
My SQL to try and get a running sum is:
SELECT qryAllocationsRolledUp.OrderID, qryAllocationsRolledUp.FillID,
qryAllocationsRolledUp.TradeDate, qryAllocationsRolledUp.Account,
qryAllocationsRolledUp.Ticker, qryAllocationsRolledUp.BS,
qryAllocationsRolledUp.Qty, qryAllocationsRolledUp.Price,
(SELECT SUM(qryAllocationsRolledUp.Qty) FROM qryAllocationsRolledUp Q1 WHERE
Q1.Account = qryAllocationsRolledUp.Account AND Q1.Ticker =
qryAllocationsRolledUp.Ticker AND Q1.FillID<=qryAllocationsRolledUp.FillID)
AS CumQty
FROM qryAllocationsRolledUp
ORDER BY qryAllocationsRolledUp.OrderID, qryAllocationsRolledUp.Account,
qryAllocationsRolledUp.FillID;
However that gives strange results. What I'm trying to do is get a running
sum of by account and Ticker (ticker's all the same above anyway) of the Qty
column. Any ideas where I'm going wrong?
Thanks.
I'm trying to use a subquery to do a running sum of Qty for a query. Here is
sample data:
OrderID FillID TradeDate Account Security BS Qty Price
2 132 23-Mar-09 A ERM9 S 17 102
2 133 23-Mar-09 A ERM9 S 36 105
2 134 23-Mar-09 A ERM9 S 12 108
2 135 23-Mar-09 B ERM9 S 10 104
2 136 23-Mar-09 B ERM9 S 23 103
2 137 23-Mar-09 B ERM9 S 7 106
My SQL to try and get a running sum is:
SELECT qryAllocationsRolledUp.OrderID, qryAllocationsRolledUp.FillID,
qryAllocationsRolledUp.TradeDate, qryAllocationsRolledUp.Account,
qryAllocationsRolledUp.Ticker, qryAllocationsRolledUp.BS,
qryAllocationsRolledUp.Qty, qryAllocationsRolledUp.Price,
(SELECT SUM(qryAllocationsRolledUp.Qty) FROM qryAllocationsRolledUp Q1 WHERE
Q1.Account = qryAllocationsRolledUp.Account AND Q1.Ticker =
qryAllocationsRolledUp.Ticker AND Q1.FillID<=qryAllocationsRolledUp.FillID)
AS CumQty
FROM qryAllocationsRolledUp
ORDER BY qryAllocationsRolledUp.OrderID, qryAllocationsRolledUp.Account,
qryAllocationsRolledUp.FillID;
However that gives strange results. What I'm trying to do is get a running
sum of by account and Ticker (ticker's all the same above anyway) of the Qty
column. Any ideas where I'm going wrong?
Thanks.