mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-13 19:14:30 +00:00
3367d241d0
Co-authored-by: Anna <42538400+adevyatova@users.noreply.github.com>
5.2 KiB
5.2 KiB
toc_priority | toc_title |
---|---|
146 | intervalLengthSum |
intervalLengthSum
Calculates the sum of the length of all ranges (segments on numeric axis) excluding intersections.
Syntax
intervalLengthSum(start, end)
Arguments
start
— The starting value of the interval. Int32, Int64, UInt32, UInt64, Float32, Float64, DateTime or Date.end
— The ending value of the interval. Int32, Int64, UInt32, UInt64, Float32, Float64, DateTime or Date.
!!! info "Note" Arguments must be of the same data type. Otherwise, an exception will be thrown.
Returned value
- Sum of the length of all ranges (segments on numeric axis) excluding intersections. Depending on the type of the argument, the return value may be UInt64 or Float64 type.
Examples
- Input table:
┌─id─┬─start─┬─end─┐
│ a │ 1.1 │ 3.2 │
│ a │ 4 │ 5 │
└────┴───────┴─────┘
In this example, the arguments of the Float32
type are used. In this case, the function returns a value of the Float64
type:
SELECT id, intervalLengthSum(start, end), toTypeName(intervalLengthSum(start, end)) FROM fl_interval GROUP BY id ORDER BY id;
Result:
┌─id─┬─segmentLengthSum(start, end)─┬─toTypeName(segmentLengthSum(start, end))─┐
│ a │ 3.1 │ Float64 │
└────┴──────────────────────────────┴──────────────────────────────────────────┘
- Input table:
┌─id─┬───────────────start─┬─────────────────end─┐
│ a │ 2020-01-01 01:12:30 │ 2020-01-01 02:50:31 │
│ a │ 2020-01-01 03:11:22 │ 2020-01-01 03:23:31 │
└────┴─────────────────────┴─────────────────────┘
In this example, the arguments of the DateTime
type are used. In this case, the function returns a value in seconds:
SELECT id, intervalLengthSum(start, end), toTypeName(intervalLengthSum(start, end)) FROM dt_interval GROUP BY id ORDER BY id;
Result:
┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
│ a │ 6610 │ UInt64 │
└────┴───────────────────────────────┴───────────────────────────────────────────┘
- Input table:
┌─id─┬──────start─┬────────end─┐
│ a │ 2020-01-01 │ 2020-01-04 │
│ a │ 2020-01-12 │ 2020-01-18 │
└────┴────────────┴────────────┘
In this example, the arguments of the Date type are used. The function returns a value in days.
Query:
SELECT id, intervalLengthSum(start, end), toTypeName(intervalLengthSum(start, end)) FROM date_interval GROUP BY id ORDER BY id;
Result:
┌─id─┬─intervalLengthSum(start, end)─┬─toTypeName(intervalLengthSum(start, end))─┐
│ a │ 9 │ UInt64 │
└────┴───────────────────────────────┴───────────────────────────────────────────┘