2017-06-14 51 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