SQL Server 2012窗口函数计算运行总计

时间:2022-04-14 08:48:54

I need some help with windowing functions.

我需要一些关于窗口函数的帮助。

I have been playing around with sql 2012 windowing functions recently. I know that you can calculate the sum within a window and the running total within a window. But i was wondering; is it possible to calculate the previous running total i.e. the running total not including the current row ? I assume you would need to use the ROW or RANGE argument and I know there is a CURRENT ROW option but I would need a CURRENT ROW - I which is invalid syntax. My knowledge of the ROW and RANGE arguments is limited so any help would be gratefully received.

我最近一直在玩sql 2012窗口函数。我知道您可以计算窗口内的总和以及窗口内的运行总和。但我在想;是否可以计算先前的运行总数,即不包括当前行的运行总数?我假设您需要使用ROW或RANGE参数,我知道有一个CURRENT ROW选项,但我需要一个CURRENT ROW - 我的语法无效。我对ROW和RANGE参数的了解是有限的,所以任何帮助都会被感激不尽。

I know that there are many solutions to this problem, but I am looking to understand the ROW, RANGE arguments and I assume the problem can be cracked with these. I have included one possible way to calculate the previous running total but I wonder if there is a better way.

我知道这个问题有很多解决方案,但我希望了解ROW,RANGE参数,我认为这些问题可以解决。我已经提供了一种可能的方法来计算以前的运行总量,但我想知道是否有更好的方法。

USE AdventureWorks2012

SELECT s.SalesOrderID
    , s.SalesOrderDetailID
    , s.OrderQty
    , SUM(s.OrderQty) OVER (PARTITION BY  SalesOrderID) AS RunningTotal
    , SUM(s.OrderQty) OVER (PARTITION BY  SalesOrderID 
                         ORDER BY SalesOrderDetailID) - s.OrderQty AS PreviousRunningTotal
    -- Sudo code - I know this does not work
    --, SUM(s.OrderQty) OVER (PARTITION BY  SalesOrderID 
    --                   ORDER BY SalesOrderDetailID
    --                   ROWS BETWEEN UNBOUNDED PRECEDING 
    --                                   AND CURRENT ROW - 1) 
    -- AS  SudoCodePreviousRunningTotal
FROM Sales.SalesOrderDetail s
WHERE SalesOrderID IN (43670, 43669, 43667, 43663)
ORDER BY s.SalesOrderID
    , s.SalesOrderDetailID 
    , s.OrderQty

Thanks in advance

提前致谢

1 个解决方案

#1


21  

You could subtract the current row's value:

您可以减去当前行的值:

SUM(s.OrderQty) OVER (PARTITION BY SalesOrderID
                      ORDER BY SalesOrderDetailID) - s.OrderQty

Or according to the syntax at MSDN and ypercube's answer:

或者根据MSDN和ypercube的答案的语法:

<window frame preceding> ::= 
{
    UNBOUNDED PRECEDING
  | <unsigned_value_specification> PRECEDING
  | CURRENT ROW
}

-->

SUM(s.OrderQty) OVER (PARTITION BY SalesOrderID
                      ORDER BY SalesOrderDetailID
                      ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)

#1


21  

You could subtract the current row's value:

您可以减去当前行的值:

SUM(s.OrderQty) OVER (PARTITION BY SalesOrderID
                      ORDER BY SalesOrderDetailID) - s.OrderQty

Or according to the syntax at MSDN and ypercube's answer:

或者根据MSDN和ypercube的答案的语法:

<window frame preceding> ::= 
{
    UNBOUNDED PRECEDING
  | <unsigned_value_specification> PRECEDING
  | CURRENT ROW
}

-->

SUM(s.OrderQty) OVER (PARTITION BY SalesOrderID
                      ORDER BY SalesOrderDetailID
                      ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)