WITH
data_source AS (
SELECT user_id, g -- group
FROM source_table
)
SELECT g
, approx_distinct(user_id) as uu
, cardinality(
merge(approx_set(user_id)) OVER (order by g)
) as uu_accumurated
, cardinality(
merge(approx_set(user_id)) OVER (order by g)
) * 1.0000
/ (cardinality(merge(approx_set(user_id)) OVER ()))
as uu_acccumurated_rate
Reference
Trino implements the approx_distinct() function using the HyperLogLog data structure.
merge(HyperLogLog) → HyperLogLog#
Returns the HyperLogLog of the aggregate union of the individual hll HyperLogLog structures.