ClickHouse/docs/en/sql-reference/functions/time-window-functions.md

118 lines
3.9 KiB
Markdown
Raw Normal View History

2021-12-07 13:35:30 +00:00
---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/functions/time-window-functions
2023-04-19 17:05:55 +00:00
sidebar_position: 175
sidebar_label: Time Window
2021-12-07 13:35:30 +00:00
---
2022-06-02 10:55:18 +00:00
# Time Window Functions
2021-12-07 13:35:30 +00:00
Time window functions return the inclusive lower and exclusive upper bound of the corresponding window. The functions for working with WindowView are listed below:
2021-12-07 13:35:30 +00:00
2022-06-02 10:55:18 +00:00
## tumble
2021-12-07 13:35:30 +00:00
2021-12-08 14:43:26 +00:00
A tumbling time window assigns records to non-overlapping, continuous windows with a fixed duration (`interval`).
2021-12-07 13:35:30 +00:00
``` sql
tumble(time_attr, interval [, timezone])
```
**Arguments**
- `time_attr` - Date and time. [DateTime](../data-types/datetime.md) data type.
- `interval` - Window interval in [Interval](../data-types/special-data-types/interval.md) data type.
2021-12-07 13:35:30 +00:00
- `timezone` — [Timezone name](../../operations/server-configuration-parameters/settings.md#server_configuration_parameters-timezone) (optional).
**Returned values**
2024-05-23 13:48:20 +00:00
- The inclusive lower and exclusive upper bound of the corresponding tumbling window. [Tuple](../data-types/tuple.md)([DateTime](../data-types/datetime.md), [DateTime](../data-types/datetime.md))`.
2021-12-07 13:35:30 +00:00
**Example**
Query:
``` sql
SELECT tumble(now(), toIntervalDay('1'))
```
Result:
``` text
┌─tumble(now(), toIntervalDay('1'))─────────────┐
│ ['2020-01-01 00:00:00','2020-01-02 00:00:00'] │
└───────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## hop
2021-12-07 13:35:30 +00:00
A hopping time window has a fixed duration (`window_interval`) and hops by a specified hop interval (`hop_interval`). If the `hop_interval` is smaller than the `window_interval`, hopping windows are overlapping. Thus, records can be assigned to multiple windows.
``` sql
hop(time_attr, hop_interval, window_interval [, timezone])
```
**Arguments**
- `time_attr` - Date and time. [DateTime](../data-types/datetime.md) data type.
- `hop_interval` - Hop interval in [Interval](../data-types/special-data-types/interval.md) data type. Should be a positive number.
- `window_interval` - Window interval in [Interval](../data-types/special-data-types/interval.md) data type. Should be a positive number.
2021-12-07 13:35:30 +00:00
- `timezone` — [Timezone name](../../operations/server-configuration-parameters/settings.md#server_configuration_parameters-timezone) (optional).
**Returned values**
2024-05-23 13:48:20 +00:00
- The inclusive lower and exclusive upper bound of the corresponding hopping window. Since one record can be assigned to multiple hop windows, the function only returns the bound of the **first** window when hop function is used **without** `WINDOW VIEW`. [Tuple](../data-types/tuple.md)([DateTime](../data-types/datetime.md), [DateTime](../data-types/datetime.md))`.
2021-12-07 13:35:30 +00:00
**Example**
Query:
``` sql
SELECT hop(now(), INTERVAL '1' SECOND, INTERVAL '2' SECOND)
```
Result:
``` text
┌─hop(now(), toIntervalSecond('1'), toIntervalSecond('2'))──┐
│ ('2020-01-14 16:58:22','2020-01-14 16:58:24') │
└───────────────────────────────────────────────────────────┘
```
2022-06-02 10:55:18 +00:00
## tumbleStart
2021-12-07 13:35:30 +00:00
2021-12-08 14:43:26 +00:00
Returns the inclusive lower bound of the corresponding tumbling window.
2021-12-07 13:35:30 +00:00
``` sql
2022-05-03 17:12:42 +00:00
tumbleStart(bounds_tuple);
2021-12-07 13:35:30 +00:00
tumbleStart(time_attr, interval [, timezone]);
```
2022-06-02 10:55:18 +00:00
## tumbleEnd
2021-12-07 13:35:30 +00:00
2021-12-08 14:43:26 +00:00
Returns the exclusive upper bound of the corresponding tumbling window.
2021-12-07 13:35:30 +00:00
``` sql
2022-05-03 17:12:42 +00:00
tumbleEnd(bounds_tuple);
2021-12-07 13:35:30 +00:00
tumbleEnd(time_attr, interval [, timezone]);
```
2022-06-02 10:55:18 +00:00
## hopStart
2021-12-07 13:35:30 +00:00
2021-12-08 14:43:26 +00:00
Returns the inclusive lower bound of the corresponding hopping window.
2021-12-07 13:35:30 +00:00
``` sql
2022-05-03 17:12:42 +00:00
hopStart(bounds_tuple);
2021-12-07 13:35:30 +00:00
hopStart(time_attr, hop_interval, window_interval [, timezone]);
```
2022-06-02 10:55:18 +00:00
## hopEnd
2021-12-07 13:35:30 +00:00
2021-12-08 14:43:26 +00:00
Returns the exclusive upper bound of the corresponding hopping window.
2021-12-07 13:35:30 +00:00
``` sql
2022-05-03 17:12:42 +00:00
hopEnd(bounds_tuple);
2021-12-07 13:35:30 +00:00
hopEnd(time_attr, hop_interval, window_interval [, timezone]);
2022-05-03 17:12:42 +00:00
```
2023-01-17 15:38:10 +00:00
## Related content
2023-04-19 17:05:55 +00:00
- Blog: [Working with time series data in ClickHouse](https://clickhouse.com/blog/working-with-time-series-data-and-functions-ClickHouse)