intervalLengthSum

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/intervalLengthSum.md)

## intervalLengthSum[​](#intervalLengthSum "Direct link to intervalLengthSum")

Introduced in: v21.7.0

Takes multiple numeric ranges and calculates the total length when all overlapping parts are combined into a single unified range.

Note

Arguments must be of the same data type.
Otherwise, an exception will be thrown.

**Syntax**

```
intervalLengthSum(start, end)
```

**Arguments**

- `start` — The starting value of the interval. [`(U)Int32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`DateTime`](/docs/sql-reference/data-types/datetime) or [`Date`](/docs/sql-reference/data-types/date)
- `end` — The ending value of the interval. [`(U)Int32/64`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`DateTime`](/docs/sql-reference/data-types/datetime) or [`Date`](/docs/sql-reference/data-types/date)

**Returned value**

Returns the total length of union of all ranges (segments on numeric axis). Depending on the type of the argument, the return value may be UInt64 or Float64 type. [`UInt64`](/docs/sql-reference/data-types/int-uint) or [`Float64`](/docs/sql-reference/data-types/float)

**Examples**

**Float32 example**

```
CREATE TABLE fl_interval (id String, start Float32, end Float32) ENGINE = Memory;
INSERT INTO fl_interval VALUES ('a', 1.1, 2.9), ('a', 2.5, 3.2), ('a', 4, 5);

SELECT id, intervalLengthSum(start, end), toTypeName(intervalLengthSum(start, end)) FROM fl_interval GROUP BY id ORDER BY id;
```

```
┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
│ a  │                           3.1 │ Float64                                   │
└────┴───────────────────────────────┴───────────────────────────────────────────┘
```

**DateTime example**

```
CREATE TABLE dt_interval (id String, start DateTime, end DateTime) ENGINE = Memory;
INSERT INTO dt_interval VALUES ('a', '2020-01-01 01:12:30', '2020-01-01 02:10:10'), ('a', '2020-01-01 02:05:30', '2020-01-01 02:50:31'), ('a', '2020-01-01 03:11:22', '2020-01-01 03:23:31');

SELECT id, intervalLengthSum(start, end), toTypeName(intervalLengthSum(start, end)) FROM dt_interval GROUP BY id ORDER BY id;
```

```
┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
│ a  │                          6610 │ UInt64                                    │
└────┴───────────────────────────────┴───────────────────────────────────────────┘
```

**Date example**

```
CREATE TABLE date_interval (id String, start Date, end Date) ENGINE = Memory;
INSERT INTO date_interval VALUES ('a', '2020-01-01', '2020-01-04'), ('a', '2020-01-12', '2020-01-18');

SELECT id, intervalLengthSum(start, end), toTypeName(intervalLengthSum(start, end)) FROM date_interval GROUP BY id ORDER BY id;
```

```
┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
│ a  │                             9 │ UInt64                                    │
└────┴───────────────────────────────┴───────────────────────────────────────────┘
```

- [intervalLengthSum](#intervalLengthSum)

Was this page helpful?