Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calculate running total(+/-) using a select statement

GaillimhOct 26 2018 — edited Oct 26 2018

Hi forum,

I'd like to keep the track of the following orders from this table using a select statement rather than a PL/SQL block

Is there a way to keep track of the running total as I add to my quantity with BUY and reduce my quantity with SELL. In PL/SQL I would return the data to a cursor,

loop through the data and add or subtract depending on a buy or sell using an IF statement. Is there a way to do this using a SELECT statement. I need to track the moving total.

I know there is a cumulative function in oracle but that assumes the next value is additive. Trying to calculate RUNNING_TOTAL from ORDER_AMOUNT

BUY/SELLORDER_AMOUNTRUNNING_TOTAL
BUY1010
BUY1525
BUY1035
BUY2055
SELL3025
SELL1510
SELL55
BUY16

BUY

5056
This post has been answered by mNem on Oct 26 2018
Jump to Answer
Comments
Post Details
Added on Oct 26 2018
11 comments
3,352 views