大网数据广告活动
数据库部署与 SQL 参考

Apache Doris 中文参考:APPROX_COUNT_DISTINCT

· 52CE 我爱测

返回非 NULL 的不同元素数量。 基于 HyperLogLog 算法实现,使用固定大小的内存估算列基数。 该算法基于尾部零分布假设进行计算,具体精确程度取决于数据分布。基于 Doris 使用的固定桶大小,该算法相对标准误差为 0.8125% 更详细具体的分析,详见相关论文

描述

返回非 NULL 的不同元素数量。 基于 HyperLogLog 算法实现,使用固定大小的内存估算列基数。 该算法基于尾部零分布假设进行计算,具体精确程度取决于数据分布。基于 Doris 使用的固定桶大小,该算法相对标准误差为 0.8125% 更详细具体的分析,详见相关论文

语法

APPROX_COUNT_DISTINCT(<expr>)
NDV(<expr>)

参数说明

参数 说明
<expr> 用于计算的表达式。支持的类型包括 String、Date、DateTime、TimestampNs、Timestamptz、IPv4、IPv6、UUID、TinyInt、SmallInt、Integer、BigInt、LargeInt、Float、Double、Decimal。

返回值

返回 BIGINT 类型的值。

举例

-- setup
create table t1(
        k1 int,
        k_string varchar(100),
        k_tinyint tinyint
) distributed by hash (k1) buckets 1
properties ("replication_num"="1");
insert into t1 values 
    (1, 'apple', 10),
    (1, 'banana', 20),
    (1, 'apple', 10),
    (2, 'orange', 30),
    (2, 'orange', 40),
    (2, 'grape', 50),
    (3, null, null);
select approx_count_distinct(k_string) from t1;

String 类型:计算所有 k_string 值的近似去重数量,NULL 值不参与计算。

+---------------------------------+
| approx_count_distinct(k_string) |
+---------------------------------+
|                               4 |
+---------------------------------+
select approx_count_distinct(k_tinyint) from t1;

TinyInt 类型:计算所有 k_tinyint 值的近似去重数量。

+----------------------------------+
| approx_count_distinct(k_tinyint) |
+----------------------------------+
|                                5 |
+----------------------------------+
select approx_count_distinct(k1) from t1;

Integer 类型:计算所有 k1 值的近似去重数量。

+---------------------------+
| approx_count_distinct(k1) |
+---------------------------+
|                         3 |
+---------------------------+
select k1, approx_count_distinct(k_string) from t1 group by k1;

按 k1 分组,计算每组中 k_string 的近似去重数量。组内记录都为 NULL 时,返回 0。

+------+---------------------------------+
| k1   | approx_count_distinct(k_string) |
+------+---------------------------------+
|    1 |                               2 |
|    2 |                               2 |
|    3 |                               0 |
+------+---------------------------------+
select ndv(k_string) from t1;

使用别名 NDV 的效果与 APPROX_COUNT_DISTINCT 相同。

+---------------+
| ndv(k_string) |
+---------------+
|             4 |
+---------------+
select approx_count_distinct(k_string) from t1 where k1 = 999;

当查询结果为空时,返回 0。

+---------------------------------+
| approx_count_distinct(k_string) |
+---------------------------------+
|                               0 |
+---------------------------------+

本文来自 Apache Doris 文档贡献者,按 Apache 2.0 整理版式与相对链接,保留正文与代码。图示请参阅原文。作者与贡献记录。

资料来源与适用范围

资料来源
Apache Doris 文档贡献者
适用范围
master 文档快照(2026-11-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
Apache 2.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。