row_cumsum()row_cumsum()

计算序列化行集中列的累计和。Calculates the cumulative sum of a column in a serialized row set.

语法Syntax

row_cumsum ( Term [, Restart] )row_cumsum ( Term [, Restart] )

  • “Term”是一个表达式,表示要求和的值。Term is an expression indicating the value to be summed. 表达式必须是以下类型之一的标量:decimalintlongrealThe expression must be a scalar of one of the following types: decimal, int, long, or real. Null Term 值不影响总和。Null Term values do not affect the sum.
  • Restart 是 bool 类型的可选参数,表示应何时重启累计运算(重新设置为 0)。Restart is an optional argument of type bool that indicates when the accumulation operation should be restarted (set back to 0). 它可用于指示数据的分区;请参阅下面的第二个示例。It can be used to indicate partitions of the data; see the second example below.

返回Returns

此函数返回其参数的累计和。The function returns the cumulative sum of its argument.

示例Examples

下面的示例演示如何计算前几个偶数整数的累计和。The following example shows how to calculate the cumulative sum of the first few even integers.

datatable (a:long) [
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10
]
| where a%2==0
| serialize cs=row_cumsum(a)
aa cscs
22 22
44 66
66 1212
88 2020
1010 3030

此示例演示了将数据分区(这里是指按 name 分区)时如何计算累计和(这里是指 salary 的累计和):This example shows how to calculate the cumulative sum (here, of salary) when the data is partitioned (here, by name):

datatable (name:string, month:int, salary:long)
[
    "Alice", 1, 1000,
    "Bob",   1, 1000,
    "Alice", 2, 2000,
    "Bob",   2, 1950,
    "Alice", 3, 1400,
    "Bob",   3, 1450,
]
| order by name asc, month asc
| extend total=row_cumsum(salary, name != prev(name))
namename 月份month salarysalary totaltotal
AliceAlice 11 10001000 10001000
AliceAlice 22 20002000 30003000
AliceAlice 33 14001400 44004400
BobBob 11 10001000 10001000
BobBob 22 19501950 29502950
BobBob 33 14501450 44004400