QUANTILE_TDIGEST_WEIGHTED (Lakehouse v1)
Computes an approximate quantile of a numeric data sequence using the t-digest algorithm. This function takes into account the weight of each sequence member. Memory consumption is log(n), where n is a number of values.
Analyze Syntax
Section titled “Analyze Syntax”func.quantile_tdigest_weighted(<levels>, <expr>, <weight_expr>)Analyze Examples
Section titled “Analyze Examples”func.quantile_tdigest_weighted([0.5, 0.8], table.sales_amount, 1).alias('sales_amounts')
| sales_amounts ||-----------------------+| [6000.0,7000.0] |SQL Syntax
Section titled “SQL Syntax”QUANTILE_TDIGEST_WEIGHTED(<level1>[, <level2>, ...])(<expr>, <weight_expr>)Arguments
Section titled “Arguments”| Arguments | Description |
|---|---|
<level n> | A level of quantile represents a constant floating-point number ranging from 0 to 1. It is recommended to use a level value in the range of [0.01, 0.99]. |
<expr> | Any numerical expression |
<weight_expr> | Any unsigned integer expression. Weight is a number of value occurrences. |
Return Type
Section titled “Return Type”Returns either a Float64 value or an array of Float64 values, depending on the number of quantile levels specified.
SQL Examples
Section titled “SQL Examples”-- Create a table and insert sample dataCREATE TABLE sales_data ( id INT, sales_person_id INT, sales_amount FLOAT);
INSERT INTO sales_data (id, sales_person_id, sales_amount)VALUES (1, 1, 5000), (2, 2, 5500), (3, 3, 6000), (4, 4, 6500), (5, 5, 7000);
SELECT QUANTILE_TDIGEST_WEIGHTED(0.5)(sales_amount, 1) AS median_sales_amountFROM sales_data;
median_sales_amount|-------------------+ 6000.0|
SELECT QUANTILE_TDIGEST_WEIGHTED(0.5, 0.8)(sales_amount, 1)FROM sales_data;
quantile_tdigest_weighted(0.5, 0.8)(sales_amount)|-------------------------------------------------+[6000.0,7000.0] |