全部產品
Search
文件中心

MaxCompute:HLL_COUNT_EXTRACT

更新時間:Nov 28, 2025

HLL_COUNT_EXTRACT函數用於從HLL++資料結構(sketch)中計算基數估計值。

注意事項

HLL_COUNT_EXTRACT/HLL_COUNT_MERGE/HLL_COUNT_MERGE_PARTIAL函數使用的BINARY資料需要來源於HLL_COUNT_INIT函數, 不能來源於其他系統或者其他方式。

命令格式

BIGINT HLL_COUNT_EXTRACT(BINARY <sketch>)

參數說明

sketch:必填,BINARY類型。HLL++ sketch,由HLL_COUNT_INIT函數產生。

傳回值說明

返回BIGINT類型的基數估計值。如果輸入sketch為 NULL, 返回0。

使用樣本

查詢每個國家中至少有一張發票的自然人數量。

SELECT
  country,
  HLL_COUNT_EXTRACT(HLL_sketch) AS distinct_customers_with_open_invoice
FROM
  (
    SELECT
    country,
    HLL_COUNT_INIT(customer_id) AS hll_sketch
    FROM values
      ('UA', 'customer_id_1', 'invoice_id_11'),
      ('BR', 'customer_id_3', 'invoice_id_31'),
      ('CZ', 'customer_id_2', 'invoice_id_22'),
      ('CZ', 'customer_id_2', 'invoice_id_23'),
      ('BR', 'customer_id_3', 'invoice_id_31'),
      ('UA', 'customer_id_2', 'invoice_id_24')
    t(country, customer_id, invoice_id)
    GROUP BY country
  );

返回結果:

+---------+--------------------------------------+
| country | distinct_customers_with_open_invoice |
+---------+--------------------------------------+
| BR      | 1                                    |
| CZ      | 1                                    |
| UA      | 2                                    |
+---------+--------------------------------------+

相關函數

HLL_COUNT_EXTRACT函數屬於HyperLogLog++函數,MaxCompute支援一系列近似彙總的HyperLogLog++函數,更多相關函數請參見HyperLogLog++函數