APPROX_COUNT_DISTINCT
函数是聚合函数,它对某一列去重后的行数进行计算,结果只能返回一个值,且该值是近似值,该函数可以进一步用于计算被引用的列的选择性。
与函数 COUNT(DISTINCT x)
相比,APPROX_COUNT_DISTINCT
返回的是近似值,所以计算速度极快。在处理大量级的数据时 COUNT(DISTINCT x)
经常要花费很长的时间,使用 APPROX_COUNT_DISTINCT
牺牲了少量的精确度,却换来了计算效率的极大提升。
语法
APPROX_COUNT_DISTINCT(expr)
参数
参数 | 说明 |
expr | 数值列。 |
返回类型
返回 NUMBER
类型数据。
示例
以下语句创建了表 employees,并向里面插入数据:
CREATE TABLE employees (manager_id INT,last_name varchar(50),hiredate varchar(50),SALARY INT);
INSERT INTO employees VALUES(100, 'Raphaely', '2017-07-01', 1700);
INSERT INTO employees VALUES(100, 'De Haan', '2018-05-01',11000);
INSERT INTO employees VALUES(100, 'Errazuriz', '2017-07-21', 1400);
INSERT INTO employees VALUES(100, 'Hartstein', '2019-05-01',14000);
INSERT INTO employees VALUES(100, 'Raphaely', '2017-07-22', 1700);
INSERT INTO employees VALUES(100, 'Weiss', '2019-07-11',13500);
INSERT INTO employees VALUES(100, 'Russell', '2019-10-05', 13000);
INSERT INTO employees VALUES(100, 'Partners', '2018-12-01',14000);
INSERT INTO employees VALUES(200, 'Ross', '2019-06-11',13500);
INSERT INTO employees VALUES(200, 'Bell', '2019-05-25', 13000);
INSERT INTO employees VALUES(200, 'Part', '2018-08-11',14000);
COMMIT;
执行以下语句:
SELECT last_name, salary, APPROX_COUNT_DISTINCT(salary) OVER (ORDER BY hiredate) "Variance"
FROM employees WHERE manager_id = 100 ORDER BY last_name, salary, "Variance";
查询结果如下:
+-----------+--------+----------+
| LAST_NAME | SALARY | Variance |
+-----------+--------+----------+
| De Haan | 11000 | 3 |
| Errazuriz | 1400 | 2 |
| Hartstein | 14000 | 4 |
| Partners | 14000 | 4 |
| Raphaely | 1700 | 1 |
| Raphaely | 1700 | 2 |
| Russell | 13000 | 6 |
| Weiss | 13500 | 5 |
+-----------+--------+----------+