如何根据特定条件总结不同的行值?

时间:2017-06-14 07:28:02

标签: mysql sql

当我在表中的月份超过行本身时,我想通过总结所有未来的销售来获取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

然而,这似乎比我想象的更复杂。这里有没有人能够快速解决这个问题?

2 个答案:

答案 0 :(得分:1)

您可以在P子句上写条件。

[0,0,0] [1,0,0] ... [n, 0,0]
[0,1,0]     ...       ...
  ...       ...       ...
[0,m,0]     ...     [n, m, 0]

答案 1 :(得分:0)

假设您有一个名为 table1

的表格

表格格式为:

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

试试这段代码,

.div1:hover~.div2 {
  color: red
}