2017-03-07 84 views
0

我想要在2015年的表格中獲得每個Actual_Sale_Date的銷售計數的運行總數,但迄今爲止我所有的嘗試都是徒勞的。我查看了所附的鏈接並模擬了這個過程,但沒有任何收穫。有人可以幫忙嗎?添加一個正在運行的總計/累計列

Calculating Cumulative Sum in PostgreSQL

select Actual_Sale_Date, extract(week from Actual_Sale_Date) as week_number, 
count(*) from mytable 
where extract(year from Actual_Sale_Date) = 2015 

結果:(請求Running_Total)

Actual_Sale_Date   week_number   count   running_total 
2015-01-04 00:00:00   1     1     1 
2015-01-06 00:00:00   2     3     4 
2015-01-08 00:00:00   2     4     8 
2015-01-09 00:00:00   2     5     13 
2015-01-11 00:00:00   2     1     14 
2015-01-15 00:00:00   3     2     16 
2015-01-21 00:00:00   4     1     17 
2015-01-23 00:00:00   4     4     21 
2015-01-24 00:00:00   4     1     22 
2015-01-26 00:00:00   5     2     24 
+0

我會別名表,然後做一個子查詢的東西,總和計算出該行的銷售日期。 – Denziloe

回答

3

只需使用窗口功能:

select Actual_Sale_Date, extract(week from Actual_Sale_Date) as week_number, 
     count(*), 
     sum(count(*)) over (order by Actual_Sale_Date) 
from ?? 
where extract(year from Actual_Sale_Date) = 2015 
group by Actual_Sale_Date, extract(week from Actual_Sale_Date);