Mendapatkan Nilai Running Total Pada SQL Server

Posted On // Leave a Comment
Perintah running total pada sql server ini biasanya digunakan pada saat kita ingin melihat nilai mutasi saldo suatu transaksi yang terus bertambah.

SQL Server 2005
select
    t.id ,
    t.somedate ,
    t.somevalue ,
    rt.runningTotal
from TestTable t
 cross apply (select sum(somevalue) as runningTotal
       from TestTable
       where somedate <= t.somedate
      ) as rt
order by t.somedate


SQL Server 2012
select
    t.id ,
    t.somedate ,
    t.somevalue ,
    rt.runningTotal
from TestTable t
 cross apply (select sum(somevalue) as runningTotal
       from TestTable
       where somedate <= t.somedate
      ) as rt
order by t.somedate

0 komentar: