简体   繁体   English

获取特定日期最近两年的滚动费用总和

[英]Get the rolling sum of cost for last 2 years on specific date

I have a query where I have to rolling sum of last 2 year actual margin/revenue for particular month. 我有一个查询,我必须将特定年份最近2年的实际利润/收入汇总。

Let's say for example the month & year of invoice is July 2019. I will need to get the SUM(ActualMargin) from July 2017 to 2019. 举例来说,发票的月份和年份是2019年7月。我将需要获得2017年7月至2019年的SUM(ActualMargin)

Does the OVER() function work here? OVER()函数在这里工作吗?

This is my previous query: 这是我以前的查询:

SELECT Agent AS IBO,
  CustomerMode AS Mode,
  MONTH(OriginalInvoiceExtractDate) AS monthInvoice,
  YEAR(OriginalInvoiceExtractDate) AS yearInvoice,
  SUM(ActualMargin) AS ActualMargin,
  SUM(TotalActualRevenue) AS TotalActualRevenue,
  (SUM(ActualMargin) / NULLIF(SUM(TotalActualRevenue), 0)) * 100 AS MarginPct
FROM table
GROUP BY Agent, CustomerMode, MONTH(OriginalInvoiceExtractDate), YEAR(OriginalInvoiceExtractDate)
ORDER BY Agent, CustomerMode

Final output would be for a Agent - Mode combination, I should get SUM(Actual Margin) where summing up the actual margin until the last 24 months from the present month. 最终输出将是“代理-模式”组合的结果,我应该得到SUM(Actual Margin) ,该值求和直到本月最后24个月的实际利润。

For a IBO, Mode combo, Monthinvoice, YearInvoice combo - need the sum of actual margin for last 24 months. 对于IBO,Mode组合,Monthinvoice,YearInvoice组合-需要最近24个月的实际保证金总和。 For eg, DALLAS_CMO - LTL - June 2016 should fetch me sum(Actual Margin) as the total of all Actual Margin from June 2014 until June 2016) 例如,DALLAS_CMO-LTL-2016年6月应获取我的总和(实际保证金)作为2014年6月至2016年6月所有实际保证金的总和)

Over should work for you as long as you are on a suitable version of SQL Server. 只要您使用的是适当版本的SQL Server,Over都应该为您工作。

Below is a version of your query with a column (ActualMargin2Years) added for the sum of actual margin over the past two years. 以下是您的查询版本,其中添加了一列(ActualMargin2Years)作为过去两年的实际保证金总和。 Note that I'm going back 23 months which makes a 24 month window including the current month. 请注意,我要回溯23个月,这将形成一个包括当前月份在内的24个月的窗口。 If as you state you want to go from July 2017 to July 2019 that's actually 25 months as the period will include July 2017, 2018 and 2019. 如果您声明要从2017年7月到2019年7月,则实际上是25个月,因为此期间将包括2017年7月,2018年和2019年。

SELECT Agent AS IBO,
  CustomerMode AS Mode,
  MONTH(OriginalInvoiceExtractDate) AS monthInvoice,
  YEAR(OriginalInvoiceExtractDate) AS yearInvoice,
  SUM(ActualMargin) AS ActualMargin,
  SUM(SUM(ActualMargin)) OVER (PARTITION BY Agent,CustomerMode ORDER BY YEAR(OriginalInvoiceExtractDate),MONTH(OriginalInvoiceExtractDate)  rows between 23 preceding an current row) AS ActualMargin2Years,
  SUM(TotalActualRevenue) AS TotalActualRevenue,
  (SUM(ActualMargin) / NULLIF(SUM(TotalActualRevenue), 0)) * 100 AS MarginPct
FROM table
GROUP BY Agent, CustomerMode, MONTH(OriginalInvoiceExtractDate), YEAR(OriginalInvoiceExtractDate)
ORDER BY Agent, CustomerMode

If you need a different window to aggregate over the following might be appropriate alternatives. 如果您需要其他窗口来汇总以下内容,则可能是合适的选择。 25 month window including the current month: 25个月的窗口,包括当月:

rows between current row and 24 preceding

24 month window excluding the current month: 除当前月份外的24个月窗口:

rows between 1 preceeding and 23 preceding

As i understand, you want get Cumulative Margin with combination key Agent and CustomerMode , in that case you may try as follows: 据我了解,您想要使用AgentAgentCustomerMode获得累积保证金 ,在这种情况下,您可以尝试如下操作:

select  t.IBO,
        t.Mode,
        yearInvoice,
        monthInvoice,
        SUM (ActualMargin) over (partition by t.IBO, t.Mode order by yearInvoice, monthInvoice) as CumulativeMargin
from (
    SELECT    Agent AS IBO,
              CustomerMode AS Mode,
              MONTH(OriginalInvoiceExtractDate) AS monthInvoice,
              YEAR(OriginalInvoiceExtractDate) AS yearInvoice,
              SUM(ActualMargin) AS ActualMargin,
              SUM(TotalActualRevenue) AS TotalActualRevenue,
              (SUM(ActualMargin) / NULLIF(SUM(TotalActualRevenue), 0)) * 100 AS MarginPct
    FROM table
    GROUP BY Agent, CustomerMode, MONTH(OriginalInvoiceExtractDate), YEAR(OriginalInvoiceExtractDate)
    ) as t 
go

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

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