Rabu, 19 April 2017

Akumulasi ( Rolling Sum ) SQL

Contoh :

Date                    Sales           Running Total Of Sales
10/1/2015             5                           5
10/2/2015             3                           8
10/3/2015             7                         15
10/4/2015             8                         23
10/5/2015             2                         25
10/6/2015             3                         28
10/7/2015             6                         34

Cara 1.
select
a.date,
sum(b.sales) as cumulative_sales
from sales_table a
join sales_table b on a.date >= b.date
group by a.date
order by a.date;

Cara 2.
select
date,
sum(sales) over (order by date rows unbounded preceding) as cumulative_sales
from sales_table;



source:https://www.wagonhq.com/blog/running-totals-sql

Tidak ada komentar:

Posting Komentar