Introduced in: v23.4.0
Computes the quantile of a numeric data sequence using the Greenwald-Khanna algorithm.
The Greenwald-Khanna algorithm is an algorithm used to compute quantiles on a stream of data in a highly efficient manner. It was introduced by Michael Greenwald and Sanjeev Khanna in 2001. It is widely used in databases and big data systems where computing accurate quantiles on a large stream of data in real-time is necessary. The algorithm is highly efficient, taking only O(log n) space and O(log log n) time per item (where n is the size of the input). It is also highly accurate, providing an approximate quantile value with high probability.
quantileGK is different from other quantile functions in ClickHouse, because it enables user to control the accuracy of the approximate quantile result.
Syntax
quantileGK(accuracy, level)(expr)Arguments
expr— Expression over the column values resulting in numeric data types,Date,DateTimeorDateTime64.(U)Int*orInt128orUInt128orInt256orUInt256orFloat*orDecimal*orDateorDateTimeorDateTime64
Returned value
Returns the quantile of the specified level and accuracy. For numeric data types the output format matches the input format. (U)Int* or Int128 or UInt128 or Int256 or UInt256 or Float* or Decimal* or Date or DateTime or DateTime64
Examples
Computing quantile with different accuracy levels
SELECT quantileGK(1, 0.25)(number + 1) FROM numbers(1000);┌─quantileGK(1, 0.25)(plus(number, 1))─┐
│ 1 │
└──────────────────────────────────────┘Higher accuracy quantile
SELECT quantileGK(100, 0.25)(number + 1) FROM numbers(1000);┌─quantileGK(100, 0.25)(plus(number, 1))─┐
│ 251 │
└────────────────────────────────────────┘