# largestTriangleThreeBuckets (/docs/reference/sql/aggregate-functions/largesttrianglethreebuckets)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

Applies the [Largest-Triangle-Three-Buckets](https://skemman.is/bitstream/1946/15343/3/SS_MSthesis.pdf) algorithm to the input data.
The algorithm is used for downsampling time series data for visualization.
It is designed to operate on series sorted by x coordinate.
It works by dividing the sorted series into buckets and then finding the largest triangle in each bucket.
The number of buckets is equal to the number of points in the resulting series.
The function will sort data by `x` and then apply the downsampling algorithm to the sorted data.

NaNs are ignored in the provided series, meaning that any NaN values will be excluded from the analysis.
This ensures that the function operates only on valid numerical data.

**Syntax**

```sql
largestTriangleThreeBuckets(n)(x, y)
```

**Parameters**

* `n` — Number of points in the resulting series. [`UInt64`](/reference/data-types/int-uint)

**Arguments**

* `x` — x coordinate. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/reference/data-types/decimal) or [`Date`](/reference/data-types/date) or [`Date32`](/reference/data-types/date32) or [`DateTime`](/reference/data-types/datetime) or [`DateTime64`](/reference/data-types/datetime64)
* `y` — y coordinate. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/reference/data-types/decimal) or [`Date`](/reference/data-types/date) or [`Date32`](/reference/data-types/date32) or [`DateTime`](/reference/data-types/datetime) or [`DateTime64`](/reference/data-types/datetime64)

**Returned value**

Returns an array of tuples with two elements.. [`Array(Tuple(Float64, Float64))`](/reference/data-types/array)

**Examples**

**Downsampling time series data**

```sql title=Query
CREATE TABLE largestTriangleThreeBuckets_test (x Float64, y Float64) ENGINE = Memory;
INSERT INTO largestTriangleThreeBuckets_test VALUES
    (1.0, 10.0), (2.0, 20.0), (3.0, 15.0), (8.0, 60.0), (9.0, 55.0),
    (10.0, 70.0), (4.0, 30.0), (5.0, 40.0), (6.0, 35.0), (7.0, 50.0);

SELECT largestTriangleThreeBuckets(4)(x, y) FROM largestTriangleThreeBuckets_test;
```

```text title=Response
┌────────largestTriangleThreeBuckets(4)(x, y)───────────┐
│           [(1,10),(3,15),(9,55),(10,70)]              │
└───────────────────────────────────────────────────────┘
```

*Introduced in version 23.10.0.*
