Apache IoTDB COUNT(DISTINCT列) 错误
在Apache IoTDB 2.0.8(树模型)中使用COUNT(DISTINCT column) 来统计不同值时遇到了SQL解析错误。我需要统计某列中唯一值的数量(例如不同的设备ID)。
测试数据
create database `root.test`;
create timeseries `root.test.d1.s1` int32;
insert into `root.test.d1`(`time`, s1) values (1, 10);
insert into `root.test.d1`(`time`, s1) values (2, 20);
insert into `root.test.d1`(`time`, s1) values (3, 10);
insert into `root.test.d1`(`time`, s1) values (4, 30);
有问题的查询
select count(distinct s1)
from `root.test.d1`;
错误信息
错误: 700: 在将SQL解析为物理计划时发生错误: 第1 行,第22列 在输入 'SELECT count(DISTINCT s1' 时没有可行的替代项
普通的COUNT查询可以正常工作
select count(*) from `root.test.d1`;
select count(s1) from `root.test.d1`;
会正确返回4。
+----------------------+
|count(`root.test.d1.s1`)|
+----------------------+
| 4|
+----------------------+
Total line number = 1
带DISTINCT的其他聚合函数也会失败
select sum(distinct s1) from `root.test.d1`;-- Parsing error
select avg(distinct s1) from `root.test.d1`;-- Parsing error
Is COUNT(DISTINCT column) not supported in Apache IoTDB?
COUNT(DISTINCT column) 在Apache IoTDB中是否不被支持?
If unsupported, what SQL can I use to count distinct values and get the expected result:
如果不支持,我可以使用什么SQL来统计不同值并得到预期的结果:
+------------------+
|count(DISTINCT s1)|
+------------------+
| 3|
+------------------+
解决方案
Unlike standard SQL, ioTDB does not support the DISTINCT modifier in its COUNT() aggregation function.
与标准SQL不同,ioTDB不支持其 COUNT() 聚合函数中的 DISTINCT 修饰符。
You should be able to get the equivalent result with a subquery that returns the distinct values.
应该能够通过一个返回唯一值的子查询来获得等效的结果。
SELECT COUNT(*)
FROM (
SELECT DISTINCT s1
FROM `root.test.d1`
) sub
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。