2017-08-12 142 views
1

我想獲得整列的累計總和。以下是我的數據樣本。累計總和不工作

DT        ETB_DT 
Friday 00:00:00.0000000 Friday 04:00:00.0000000 
Friday 00:00:00.0000000 Friday 08:00:00.0000000 
Friday 00:00:00.0000000 Friday 12:00:00.0000000 
Friday 00:00:00.0000000 Friday 16:00:00.0000000 
Friday 00:00:00.0000000 Friday 20:00:00.0000000 

No_of_records 
22    
41 
86 
65 
25 

我已經使用下面的代碼來獲取應該添加到前一行的累積列。

Select * 
     ,cumulative = sum(No_of_records) 
From withoutcumulative4 
group by DT,ETB_DT,No_of_records 
    Order by DT 

但是,這給了我以下輸出。

DT        ETB_DT 
Friday 00:00:00.0000000 Friday 04:00:00.0000000 
Friday 00:00:00.0000000 Friday 08:00:00.0000000 
Friday 00:00:00.0000000 Friday 12:00:00.0000000 
Friday 00:00:00.0000000 Friday 16:00:00.0000000 
Friday 00:00:00.0000000 Friday 20:00:00.0000000 

No_of_records cumulative 
22     22 
41     41 
86     86 
65     65 
25     25 

以下是我的理想輸出。

DT        ETB_DT 
Friday 00:00:00.0000000 Friday 04:00:00.0000000 
Friday 00:00:00.0000000 Friday 08:00:00.0000000 
Friday 00:00:00.0000000 Friday 12:00:00.0000000 
Friday 00:00:00.0000000 Friday 16:00:00.0000000 
Friday 00:00:00.0000000 Friday 20:00:00.0000000 

No_of_records cumulative 
22     22 
41     63 
86     149 
65     214 
25     239 

你們有沒有想法?非常感謝您的幫助。

回答

0

這是累積和語法:

Select DT, ETB_DT, No_of_records 
     sum(No_of_records) over (order by DT, ETB_DT) as cumulative 
From withoutcumulative4 
Order by DT; 

over子句order by是什麼使之和累計。不分配它的名字。我可以告訴你,你不需要group by

+0

謝謝你這個工作! – Faheera

0

您可以使用在:

Select * 
     ,cumulative = sum(No_of_records) over (order by ETB_DT) 
From withoutcumulative4 
    Order by DT