2017-06-14 66 views
-2

我試圖通過總結所有未來的銷售情況來獲得FutureSales字段,當表中的月份超過行本身時。如何基於特定條件總結不同的行值?

Year Month Month_No Sales FutureSales 
2015 Jan  1   5  106 
2015 Feb  2   15  101 
2015 Mar  3   21  86 
2015 Apr  4   4  65 
2015 May  5   10  61 
2015 Jun  6   51  51 

但是,這似乎比我想象的更復雜。有沒有人有任何快速解決這個問題的方法?

+1

以「一個月的時候,是什麼意思在表格比行本身更多「? – ProgrammingBaKa

+0

即時通訊試圖說的是,如果在第2個月,有任何月份> = 2的行將被納入未來銷售 – Ben

回答

1

您可以在where子句中寫入條件。

select sum(some_data) 
    from your_table 
where _here_you_limit_the_rows_to_be_processed_ 
+0

感謝您的評論。但是,主要關心的是將不同行的值相加 – Ben

0

假設你有一個表稱爲表1

其表的格式是:

Year Month Month_No Sales 
2015 Jan  1   5  
2015 Feb  2   15  
2015 Mar  3   21  
2015 Apr  4   4  
2015 May  5   10 
2015 Jun  6   51 

試試這個代碼,

select a.Year, a.Month, a.Month_no, a.Sales, 
(select sum(Sales) from table1 where Month_No >= a.Month_no and Year = a.Year) as FutureSales 
from table1 a