# deltaSum (/docs/reference/sql/aggregate-functions/deltasum)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

Sums the arithmetic difference between consecutive rows.
If the difference is negative, it is ignored.

:::tip
The underlying data must be sorted for this function to work properly.
If you would like to use this function in a [materialized view](/reference/statements/create/view#materialized-view), you most likely want to use the [`deltaSumTimestamp`](/reference/functions/aggregate-functions/deltaSumTimestamp) function instead.
:::

See also:

* [`runningDifference`](/reference/functions/regular-functions/other-functions#runningDifference)

**Syntax**

```sql
deltaSum(x1[, x2, ...])
```

**Arguments**

* `x1[, x2, ...]` — One or more input values. [`Integer`](/reference/data-types/int-uint) or [`Float`](/reference/data-types/float)

**Returned value**

Returns a gained arithmetic difference of the input values. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Examples**

**Basic usage with positive differences**

```sql title=Query
SELECT deltaSum(arrayJoin([1, 2, 3]))
```

```text title=Response
┌─deltaSum(arrayJoin([1, 2, 3]))─┐
│                              2 │
└────────────────────────────────┘
```

**Mixed values with negative differences ignored**

```sql title=Query
SELECT deltaSum(arrayJoin([1, 2, 3, 0, 3, 4, 2, 3]))
```

```text title=Response
┌─deltaSum(arrayJoin([1, 2, 3, 0, 3, 4, 2, 3]))─┐
│                                             7 │
└───────────────────────────────────────────────┘
```

**Float values**

```sql title=Query
SELECT deltaSum(arrayJoin([2.25, 3, 4.5]))
```

```text title=Response
┌─deltaSum(arrayJoin([2.25, 3, 4.5]))─┐
│                                2.25 │
└─────────────────────────────────────┘
```

*Introduced in version 21.3.0.*
