上个月的数据公式

公交车

我有一个自定义表,该表具有MS SQL 2014中的以下信息。我试图提出一种计算以下公式的方法。

ABS(LastMonthTotal + LastMonthNew – ThisMonthTotal)=提成

即ABS(AugustTotal + AugustNew – SeptemberTotal)。这将产生557。

我当时在考虑在ABS中使用case语句,但是无法使其正常工作。

  creationmonth MonthCreated    TotalCount  NewCount    RetreivedON

  8             August          11238       1629        8/1/2016 0:00

  9             September       12310       721         9/1/2016 0:00

到目前为止,这是我想出的,但是没有用。我希望在使该部分正常工作之后,可以使动态部分能够正常工作以查找本月和上个月。

abs((CreationMonth ='8'然后totalcount结束的情况)+(CreationMonth ='8'然后newcount结束的情况--(creatationmonth ='9'然后totalcount结束的情况))

马特·佩里

使用窗口功能的绝佳机会:

SELECT 
 LAG(TotalCount) OVER (ORDER BY CreationMonth) AS LastMonthTotal 
,LAG(NewCount) OVER (ORDER BY CreationMonth) AS LastMonthNew 
,TotalCount AS ThisMonthTotal
,ABS(LAG(TotalCount) OVER (ORDER BY CreationMonth) 
   + LAG (NewCount) OVER (ORDER BY CreationMonth) 
   - TotalCount) AS Decoms
from TableX;

LAG函数将为您提供前一行的值。“上一行”由OVER子句中的顺序定义在这种情况下,您只需在creationmonth之前订购即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章