views:

83

answers:

0

Hi, I have a table "AuctionResults" like below

Auction  Action    Shares  ProfitperShare   
------------------------------------------- 
Round1   BUY        6      200    
Round2   BUY        5      100   
Round2   SELL      -2      50   
Round3   SELL      -5      80

Now I need to aggregate results by every auction with BUYS after netting out SELLS in subsequent rounds on a "First Come First Net basis"

so in Round1 I bought 6 Shares and then sold 2 in Round2 and rest "4" in Round3 with a total NET profit of 6 * 200-2 * 50-4 * 80 = 780

and in Round2 I bought 5 shares and sold "1" in Round3(because earlier "4" belonged to Round1) with a NET Profit of 5 * 100-1 * 80 = 420

...so the Resulting Output should look like:

Auction   NetProfit
------------------
Round1    780    
Round2    420

Can we do this using just Oracle SQL(10g) and not PL-SQL

Thanks in advance