Calculate running total / running balance

后端 未结 6 1855
粉色の甜心
粉色の甜心 2020-11-22 10:01

I have a table:

create table Transactions(Tid int,amt int)

With 5 rows:

insert into Transactions values(1, 100)
insert into         


        
6条回答
  •  余生分开走
    2020-11-22 10:37

    select v.ID
    ,CONVERT(VARCHAR(10), v.EntryDate, 103) + ' '  + convert(VARCHAR(8), v.EntryDate, 14) 
    as EntryDate
    ,case
    when v.CreditAmount<0
    then
        ISNULL(v.CreditAmount,0) 
        else 
        0 
    End  as credit
    ,case
    when v.CreditAmount>0
    then
        v.CreditAmount
        else
        0
    End  as debit
    ,Balance = SUM(v.CreditAmount) OVER (ORDER BY v.ID ROWS UNBOUNDED PRECEDING)
          from VendorCredit v
        order by v.EntryDate desc
    

提交回复
热议问题