作为基线,这是未经任何修改的输出,使用 1 到 100 之间的数字输入:
SELECT APPROX_QUANTILES(x, 4) AS output
FROM UNNEST(GENERATE_ARRAY(1, 100)) AS x;
+----------------------------+
| output |
+----------------------------+
| ["1","25","50","75","100"] |
+----------------------------+
输出包括最小值 (1) 和最大值 (100)。如果您只想要四分位数,则需要从数组中删除它们。为了可读性/可组合性,最好使用临时 SQL UDF 来执行此操作。这里我使用INT64
元素类型,但你可以有不同的元素类型,或者:
CREATE TEMP FUNCTION StripFirstLast(arr ARRAY<INT64>) AS (
ARRAY(SELECT x FROM UNNEST(arr) AS x WITH OFFSET
WHERE OFFSET BETWEEN 1 AND ARRAY_LENGTH(arr) - 2)
);
SELECT
APPROX_QUANTILES(x, 4) AS output,
StripFirstLast(APPROX_QUANTILES(x, 4)) AS quartiles
FROM UNNEST(GENERATE_ARRAY(1, 100)) AS x;
+----------------------------+------------------+
| output | quartiles |
+----------------------------+------------------+
| ["1","25","50","75","100"] | ["25","50","75"] |
+----------------------------+------------------+
您可以看到该quartiles
数组仅包含所需的值。