IoTDB 原生支持 SQL 标准集合操作,包括 UNION(并集)、INTERSECT(交集)和EXCEPT(差集)三种核心运算符。通过执行这些操作,可实现无缝合并、比较和筛选多源时序数据查询结果,显著提升时序数据分析的灵活性与效率。
注意:该功能从 V2.0.9-beta 版本开始提供。
UNION 操作将两个查询结果集的所有行合并(不保证结果顺序),支持去重(默认)和保留重复两种模式。
query UNION (ALL | DISTINCT) query
说明:
去重规则:
UNION 或 UNION DISTINCT):自动去除重复行。UNION ALL:保留所有行(包括重复项),性能更高。输入要求:
INT32、INT64、FLOAT、DOUBLE 之间完全兼容。TEXT 与 STRING 完全兼容。INT64 与 TIMESTAMP 兼容。结果集规则:
以示例数据为原始数据。
select device_id,temperature from table1 where temperature is not null union select device_id,temperature from table2 where temperature is not null; --等价于; select device_id,temperature from table1 where temperature is not null union distinct select device_id,temperature from table2 where temperature is not null;
执行结果:
+---------+-----------+ |device_id|temperature| +---------+-----------+ | 101| 90.0| | 101| 85.0| | 100| 90.0| | 100| 85.0| | 100| 88.0| +---------+-----------+ Total line number = 5 It costs 0.074s
select device_id,temperature from table1 where temperature is not null union all select device_id,temperature from table2 where temperature is not null;
执行结果:
+---------+-----------+ |device_id|temperature| +---------+-----------+ | 101| 90.0| | 101| 90.0| | 101| 85.0| | 101| 85.0| | 101| 85.0| | 101| 85.0| | 100| 90.0| | 100| 85.0| | 100| 85.0| | 100| 88.0| | 100| 90.0| | 100| 90.0| | 101| 90.0| | 101| 85.0| | 101| 85.0| | 100| 85.0| | 100| 90.0| +---------+-----------+ Total line number = 17 It costs 0.108s
注意:
- 集合操作不保证结果顺序,实际输出顺序可能与示例不同。
INTERSECT 操作返回两个查询结果集中共同存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。
query1 INTERSECT [ALL | DISTINCT] query2
说明:
去重规则:
INTERSECT 或 INTERSECT DISTINCT):自动去除重复行。INTERSECT ALL:保留所有重复行(包括重复项),性能略低。优先级规则:
INTERSECT 优先级高于 UNION 和 EXCEPT(如 A UNION B INTERSECT C 等价于 A UNION (B INTERSECT C))。A INTERSECT B INTERSECT C 等价于 (A INTERSECT B) INTERSECT C)。输入要求:
INT32、INT64、FLOAT、DOUBLE 之间完全兼容。TEXT 与 STRING 完全兼容。INT64 与 TIMESTAMP 兼容。NULL IS NOT DISTINCT FROM NULL)。SELECT 未包含 time 列,则 time 列不参与比较,结果集无 time 列。结果集规则:
基于 示例数据:
获取 table1 和 table2 中设备及温度的共同数据(去重)
select device_id, temperature from table1 intersect select device_id, temperature from table2; --等价于; select device_id, temperature from table1 intersect distinct select device_id, temperature from table2;
执行结果:
+---------+-----------+ |device_id|temperature| +---------+-----------+ | 101| 90.0| | 101| 85.0| | 100| null| | 100| 90.0| | 100| 85.0| +---------+-----------+ Total line number = 5 It costs 0.087s
获取 table1 和 table2 中设备及温度的共同数据(保留重复)
select device_id, temperature from table1 intersect all select device_id, temperature from table2;
执行结果:
+---------+-----------+ |device_id|temperature| +---------+-----------+ | 100| 85.0| | 100| 90.0| | 100| null| | 101| 85.0| | 101| 85.0| | 101| 90.0| +---------+-----------+ Total line number = 6 It costs 0.139s
注意:
- 集合操作不保证结果顺序,实际输出顺序可能与示例不同。
- 与
UNION/EXCEPT混合使用时,需通过括号明确优先级(如A INTERSECT (B UNION C))。
EXCEPT 操作返回第一个查询结果集存在但第二个查询结果集中不存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。
query1 EXCEPT [ALL | DISTINCT] query2
说明:
去重规则:
EXCEPT 或 EXCEPT DISTINCT):自动去除重复行。EXCEPT ALL:保留所有重复行(包括重复项),性能略低。优先级规则:
EXCEPT 与 UNION 优先级相同,低于 INTERSECT(如 A INTERSECT B EXCEPT C 等价于 (A INTERSECT B) EXCEPT C)。A EXCEPT B EXCEPT C 等价于 (A EXCEPT B) EXCEPT C)。输入要求:
INT32、INT64、FLOAT、DOUBLE 之间完全兼容。TEXT 与 STRING 完全兼容。INT64 与 TIMESTAMP 兼容。NULL IS NOT DISTINCT FROM NULL)。SELECT 未包含 time 列,则 time 列不参与比较,结果集无 time 列。结果集规则:
基于 示例数据:
获取 table1 中存在但 table2 中不存在的设备及温度数据(去重)
select device_id, temperature from table1 except select device_id, temperature from table2; --等价于; select device_id, temperature from table1 except distinct select device_id, temperature from table2;
执行结果:
+---------+-----------+ |device_id|temperature| +---------+-----------+ | 101| null| | 100| 88.0| +---------+-----------+ Total line number = 2 It costs 0.173s
获取 table1 中存在但 table2 中不存在的设备及温度数据(保留重复)
select device_id, temperature from table1 except all select device_id, temperature from table2;
执行结果:
+---------+-----------+ |device_id|temperature| +---------+-----------+ | 100| 85.0| | 100| 88.0| | 100| 90.0| | 100| 90.0| | 100| null| | 101| 85.0| | 101| 85.0| | 101| 90.0| | 101| null| | 101| null| | 101| null| | 101| null| +---------+-----------+ Total line number = 12 It costs 0.155s
注意:
- 集合操作不保证结果顺序,实际输出顺序可能与示例不同。
- 与
UNION/INTERSECT混合使用时,需通过括号明确优先级(如A EXCEPT (B INTERSECT C))。