intervalLengthSum

Calculates the total length of union of all ranges (segments on numeric axis).

Syntax

  1. intervalLengthSum(start, end)

Arguments

Note

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

Returned value

  • 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.

Examples

  1. Input table:
  1. ┌─id─┬─start─┬─end─┐
  2. a 1.1 2.9
  3. a 2.5 3.2
  4. a 4 5
  5. └────┴───────┴─────┘

In this example, the arguments of the Float32 type are used. The function returns a value of the Float64 type.

Result is the sum of lengths of intervals [1.1, 3.2] (union of [1.1, 2.9] and [2.5, 3.2]) and [4, 5]

Query:

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

Result:

  1. ┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
  2. a 3.1 Float64
  3. └────┴───────────────────────────────┴───────────────────────────────────────────┘
  1. Input table:
  1. ┌─id─┬───────────────start─┬─────────────────end─┐
  2. a 2020-01-01 01:12:30 2020-01-01 02:10:10
  3. a 2020-01-01 02:05:30 2020-01-01 02:50:31
  4. a 2020-01-01 03:11:22 2020-01-01 03:23:31
  5. └────┴─────────────────────┴─────────────────────┘

In this example, the arguments of the DateTime type are used. The function returns a value in seconds.

Query:

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

Result:

  1. ┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
  2. a 6610 UInt64
  3. └────┴───────────────────────────────┴───────────────────────────────────────────┘
  1. Input table:
  1. ┌─id─┬──────start─┬────────end─┐
  2. a 2020-01-01 2020-01-04
  3. a 2020-01-12 2020-01-18
  4. └────┴────────────┴────────────┘

In this example, the arguments of the Date type are used. The function returns a value in days.

Query:

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

Result:

  1. ┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
  2. a 9 UInt64
  3. └────┴───────────────────────────────┴───────────────────────────────────────────┘