2023-11-02 20:37:07 +00:00
---
slug: /en/sql-reference/functions/time-series-functions
2023-11-07 19:56:34 +00:00
sidebar_position: 172
2023-11-02 20:37:07 +00:00
sidebar_label: Time Series
---
2024-01-05 19:39:52 +00:00
# Time Series Functions
2023-11-02 20:37:07 +00:00
2024-01-31 18:35:08 +00:00
Below functions are used for series data analysis.
2023-11-02 20:37:07 +00:00
2024-01-18 15:26:32 +00:00
## seriesOutliersDetectTukey
2023-12-05 15:44:42 +00:00
2024-02-01 17:24:12 +00:00
Detects outliers in series data using [Tukey Fences ](https://en.wikipedia.org/wiki/Outlier#Tukey%27s_fences ).
2023-12-05 15:44:42 +00:00
**Syntax**
``` sql
2024-01-18 15:26:32 +00:00
seriesOutliersDetectTukey(series);
2024-02-01 17:24:12 +00:00
seriesOutliersDetectTukey(series, min_percentile, max_percentile, K);
2023-12-05 15:44:42 +00:00
```
**Arguments**
2024-01-31 18:35:08 +00:00
- `series` - An array of numeric values.
2024-02-29 12:15:25 +00:00
- `min_percentile` - The minimum percentile to be used to calculate inter-quantile range [(IQR) ](https://en.wikipedia.org/wiki/Interquartile_range ). The value must be in range [0.02,0.98]. The default is 0.25.
- `max_percentile` - The maximum percentile to be used to calculate inter-quantile range (IQR). The value must be in range [0.02,0.98]. The default is 0.75.
2024-02-02 15:46:36 +00:00
- `K` - Non-negative constant value to detect mild or stronger outliers. The default value is 1.5.
2024-01-31 18:35:08 +00:00
At least four data points are required in `series` to detect outliers.
2023-12-05 15:44:42 +00:00
**Returned value**
2024-02-01 17:24:12 +00:00
- Returns an array of the same length as the input array where each value represents score of possible anomaly of corresponding element in the series. A non-zero score indicates a possible anomaly.
2023-12-05 15:44:42 +00:00
Type: [Array ](../../sql-reference/data-types/array.md ).
**Examples**
Query:
``` sql
2024-01-31 18:35:08 +00:00
SELECT seriesOutliersDetectTukey([-3, 2, 15, 3, 5, 6, 4, 5, 12, 45, 12, 3, 3, 4, 5, 6]) AS print_0;
2023-12-05 15:44:42 +00:00
```
Result:
``` text
2024-02-01 17:24:12 +00:00
┌───────────print_0─────────────────┐
│[0,0,0,0,0,0,0,0,0,27,0,0,0,0,0,0] │
└───────────────────────────────────┘
2023-12-05 15:44:42 +00:00
```
Query:
``` sql
2024-02-29 12:15:25 +00:00
SELECT seriesOutliersDetectTukey([-3, 2, 15, 3, 5, 6, 4.50, 5, 12, 45, 12, 3.40, 3, 4, 5, 6], 0.2, 0.8, 1.5) AS print_0;
2023-12-05 15:44:42 +00:00
```
Result:
``` text
2024-02-01 17:24:12 +00:00
┌─print_0──────────────────────────────┐
│ [0,0,0,0,0,0,0,0,0,19.5,0,0,0,0,0,0] │
└──────────────────────────────────────┘
2023-12-05 15:44:42 +00:00
```
2023-11-07 19:56:34 +00:00
## seriesPeriodDetectFFT
2023-11-02 20:37:07 +00:00
2024-01-31 18:35:08 +00:00
Finds the period of the given series data data using FFT
2023-12-14 16:34:37 +00:00
FFT - [Fast Fourier transform ](https://en.wikipedia.org/wiki/Fast_Fourier_transform )
2023-11-02 20:37:07 +00:00
**Syntax**
``` sql
2023-11-07 19:56:34 +00:00
seriesPeriodDetectFFT(series);
2023-11-02 20:37:07 +00:00
```
**Arguments**
- `series` - An array of numeric values
**Returned value**
2024-01-31 18:35:08 +00:00
- A real value equal to the period of series data
2023-12-08 15:10:47 +00:00
- Returns NAN when number of data points are less than four.
2023-11-02 20:37:07 +00:00
Type: [Float64 ](../../sql-reference/data-types/float.md ).
**Examples**
Query:
``` sql
2023-11-07 19:56:34 +00:00
SELECT seriesPeriodDetectFFT([1, 4, 6, 1, 4, 6, 1, 4, 6, 1, 4, 6, 1, 4, 6, 1, 4, 6, 1, 4, 6]) AS print_0;
2023-11-02 20:37:07 +00:00
```
Result:
``` text
┌───────────print_0──────┐
│ 3 │
└────────────────────────┘
2023-11-09 21:16:38 +00:00
```
2023-12-11 16:43:40 +00:00
``` sql
SELECT seriesPeriodDetectFFT(arrayMap(x -> abs((x % 6) - 3), range(1000))) AS print_0;
```
Result:
``` text
┌─print_0─┐
│ 6 │
└─────────┘
```
2023-11-21 20:25:21 +00:00
## seriesDecomposeSTL
2024-01-31 18:35:08 +00:00
Decomposes a series data using STL [(Seasonal-Trend Decomposition Procedure Based on Loess) ](https://www.wessa.net/download/stl.pdf ) into a season, a trend and a residual component.
2023-11-21 20:25:21 +00:00
**Syntax**
``` sql
seriesDecomposeSTL(series, period);
```
**Arguments**
- `series` - An array of numeric values
2024-01-08 18:49:09 +00:00
- `period` - A positive integer
2023-11-21 20:25:21 +00:00
2024-01-03 17:34:58 +00:00
The number of data points in `series` should be at least twice the value of `period` .
2024-01-03 17:16:32 +00:00
2023-11-21 20:25:21 +00:00
**Returned value**
2024-01-18 20:08:29 +00:00
- An array of four arrays where the first array include seasonal components, the second array - trend,
2024-01-19 18:20:01 +00:00
the third array - residue component, and the fourth array - baseline(seasonal + trend) component.
2023-11-21 20:25:21 +00:00
Type: [Array ](../../sql-reference/data-types/array.md ).
**Examples**
Query:
``` sql
SELECT seriesDecomposeSTL([10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34], 3) AS print_0;
```
Result:
``` text
┌───────────print_0──────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ [[
-13.529999, -3.1799996, 16.71, -13.53, -3.1799996, 16.71, -13.53, -3.1799996,
16.71, -13.530001, -3.18, 16.710001, -13.530001, -3.1800003, 16.710001, -13.530001,
-3.1800003, 16.710001, -13.530001, -3.1799994, 16.71, -13.529999, -3.1799994, 16.709997
],
[
23.63, 23.63, 23.630003, 23.630001, 23.630001, 23.630001, 23.630001, 23.630001,
23.630001, 23.630001, 23.630001, 23.63, 23.630001, 23.630001, 23.63, 23.630001,
23.630001, 23.63, 23.630001, 23.630001, 23.630001, 23.630001, 23.630001, 23.630003
],
[
0, 0.0000019073486, -0.0000019073486, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.0000019073486, 0,
0
2024-01-18 20:08:29 +00:00
],
[
10.1, 20.449999, 40.340004, 10.100001, 20.45, 40.34, 10.100001, 20.45, 40.34, 10.1, 20.45, 40.34,
10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.1, 20.45, 40.34, 10.100002, 20.45, 40.34
2023-11-21 20:25:21 +00:00
]] │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```