简体   繁体   English

如何获得累计和条件

[英]How to get cumulative sum with condition



Here is desire output : 这是欲望输出:
在此输入图像描述
I want to cum_sum the STEPID value (0-99). 我想要cum_sum的STEPID值(0-99)。 BUT, there are two pattern as can see from pic above. 但是,从上面的图片可以看出有两种模式。

Set LEFT : will multiply*1000 when STEPID is 14. And will continue with cum_sum until STEPID is 99. 设置LEFT:当STEPID为14时将乘以* 1000.并将继续使用cum_sum直到STEPID为99。
Set RIGHT : normal cumulative sum until STEPID 99. 设置右:正常累积总和,直到步骤99。

Fiddle : http://sqlfiddle.com/#!6/06e4c/1 小提琴: http ://sqlfiddle.com/#!6/06e4c/1

If this is a SQL question then you can use a CASE syntax in your Query. 如果这是一个SQL问题,那么您可以在Query中使用CASE语法。 Here is an example of the method: 以下是该方法的示例:

Select Sum(Case when X < Y Then A when X=Y then B Else C end) From Table

This allows you to add up the values only when you think that they apply. 这允许您仅在认为它们适用时才添加值。 You can also so math in the output as well. 您也可以在输出中进行数学运算。

Select Sum(Case when X <= Y Then A Else B*1000 end) From Table

See: https://msdn.microsoft.com/en-us/library/ms181765.aspx 请参阅: https//msdn.microsoft.com/en-us/library/ms181765.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM