I want to make calculation via LIFO (last in first out) method with TSQL.

Using LIFO method will require you to calculate profit/loss by selling last transactions.

Example how it works:

1. deal is concluded on 1 march we BUY 10 stocks for 5 dollars each
2. deal is concluded on 2 march we BUY 15 stocks for 6 dollars each
3. deal is concluded on 3 march we BUY 5 stocks for 4 dollars each
4. deal is concluded on 4 march we SELL 17 stocks for 7 dollars each

By the 4th transaction we now have sold 5 stocks from 3rd march for 4 dollars each and 12 stocks from 2nd march 6 dollars each. 

So now we have left the following:
10 stocks for 5 dollars each from transaction on 1 march
3 stocks from 6 dollars each from transaction on 2 march (17-5-15 = -3).

Having 13 stocks left with an average price of (10\*5 + 3\*6) / 13 = 5.23076923

Here's test data generation scripts:

```
use TestTask
go
IF OBJECT_ID('testtable','U')IS NOT NULL
        DROP TABLE testtable
go
create table testtable
(
stockid int not null,
dealid int identity (1,1) not null,
dealtype char(1) not null,
stockdate datetime not null,
stockamount int not null,
priceperstock int not null
)
insert into testtable(stockid,dealtype,stockdate,stockamount,priceperstock)
VALUES
    (111,'B','01.03.2016',10,5),
    (111,'B','02.03.2016',15,6),
    (111,'B','03.03.2016',5,4),
    (111,'S','04.03.2016',17,7)

```

I would like to calculate finance position and many other parameters which would requre me to know how many stocks with appropriate price left. I have come to this so far:

```
select
stockid,
dealid,
dealtype,
stockdate,
priceperstock,
case dealtype
    when 'B' then stockamount
    when 'S' then -stockamount
end as stockamount,
sum(
        case dealtype
            when 'B' then stockamount
            when 'S' then -stockamount
        end
    ) over (partition by 
        stockid order by dealid ROWS UNBOUNDED PRECEDING)
         as poistion
from testtable

```

Output:

```
stockid dealid  dealtype       stockdate           priceperstock    stockamount    poistion
    111       1       B       2016-01-03 00:00:00.000      5             10             10
    111       2       B       2016-02-03 00:00:00.000      6             15             25
    111       3       B       2016-03-03 00:00:00.000      4             5              30
    111       4       S       2016-04-03 00:00:00.000      7            -17             13

```

Desired output:

```
stockid dealid  dealtype       stockdate           priceperstock    stockamount    poistion    stocksleft
    111       1       B       2016-01-03 00:00:00.000      5             10             10      10
    111       2       B       2016-02-03 00:00:00.000      6             15             25      3
    111       3       B       2016-03-03 00:00:00.000      4             5              30      0
    111       4       S       2016-04-03 00:00:00.000      7            -17             13      0

```

What is the best way to go?