quantileTDigestWeighted

[Edit this page](https://github.com/ClickHouse/ClickHouse/tree/master/docs/en/sql-reference/aggregate-functions/reference/quantileTDigestWeighted.md)

## quantileTDigestWeighted[​](#quantileTDigestWeighted "Direct link to quantileTDigestWeighted")

Introduced in: v20.1.0

Computes an approximate [quantile](https://en.wikipedia.org/wiki/Quantile) of a numeric data sequence using the [t-digest](https://github.com/tdunning/t-digest/blob/master/docs/t-digest-paper/histo.pdf) algorithm.
The function takes into account the weight of each sequence member.

The maximum error is 1%.
Memory consumption is `log(n)`, where `n` is a number of values.

The performance of the function is lower than performance of [`quantile`](/docs/sql-reference/aggregate-functions/reference/quantile) or [`quantileTiming`](/docs/sql-reference/aggregate-functions/reference/quantiletiming).
In terms of the ratio of State size to precision, this function is much better than `quantile`.

The result depends on the order of running the query, and is nondeterministic.

When using multiple `quantile*` functions with different levels in a query, the internal states are not combined (that is, the query works less efficiently than it could).
In this case, use the [`quantiles`](/docs/sql-reference/aggregate-functions/reference/quantiles#quantiles) function.

Note

Using `quantileTDigestWeighted` [is not recommended for tiny data sets](https://github.com/tdunning/t-digest/issues/167#issuecomment-828650275) and can lead to significant error.
In this case, consider possibility of using [`quantileTDigest`](/docs/sql-reference/aggregate-functions/reference/quantiletdigest) instead.

**Syntax**

```
quantileTDigestWeighted(level)(expr, weight)
```

**Aliases**: `medianTDigestWeighted`

**Parameters**

- `level` — Optional. Level of quantile. Constant floating-point number from 0 to 1. We recommend using a `level` value in the range of `[0.01, 0.99]`. Default value: 0.5. At `level=0.5` the function calculates median. [`Float*`](/docs/sql-reference/data-types/float)

**Arguments**

- `expr` — Expression over the column values resulting in numeric data types, Date or DateTime. [`(U)Int*`](/docs/sql-reference/data-types/int-uint) or [`Float*`](/docs/sql-reference/data-types/float) or [`Decimal*`](/docs/sql-reference/data-types/decimal) or [`Date`](/docs/sql-reference/data-types/date) or [`DateTime`](/docs/sql-reference/data-types/datetime)
- `weight` — Column with weights of sequence elements. Weight is a number of value occurrences. [`UInt*`](/docs/sql-reference/data-types/int-uint)

**Returned value**

Approximate quantile of the specified level. [`Float64`](/docs/sql-reference/data-types/float) or [`Date`](/docs/sql-reference/data-types/date) or [`DateTime`](/docs/sql-reference/data-types/datetime)

**Examples**

**Computing weighted quantile with t-digest**

```
SELECT quantileTDigestWeighted(number, 1) FROM numbers(10);
```

```
┌─quantileTDigestWeighted(number, 1)─┐
│                                4.5 │
└────────────────────────────────────┘
```

**See Also**

- [median](/docs/sql-reference/aggregate-functions/reference/median)
- [quantiles](/docs/sql-reference/aggregate-functions/reference/quantiles)

- [quantileTDigestWeighted](#quantileTDigestWeighted)

Was this page helpful?