比较运算符和函数

基本比较运算符

  • 输入数据类型: INT32, INT64, FLOAT, DOUBLE。
  • 注意:会将所有数据转换为DOUBLE类型后进行比较。==和!=可以直接比较两个BOOLEAN。
  • 返回类型:BOOLEAN。
运算符 含义
> 大于
>= 大于等于
< 小于
<= 小于等于
== 等于
!= / <> 不等于

示例:

1. select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test;

运行结果

1. IoTDB> select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test;
2. +-----------------------------+-----------+-----------+----------------+--------------------------+---------------------------+------------------------------------------------+
3. |                         Time|root.test.a|root.test.b|root.test.a > 10|root.test.a <= root.test.b|!root.test.a <= root.test.b|(root.test.a > 10) & (root.test.a > root.test.b)|
4. +-----------------------------+-----------+-----------+----------------+--------------------------+---------------------------+------------------------------------------------+
5. |1970-01-01T08:00:00.001+08:00|         23|       10.0|            true|                     false|                       true|                                            true|
6. |1970-01-01T08:00:00.002+08:00|         33|       21.0|            true|                     false|                       true|                                            true|
7. |1970-01-01T08:00:00.004+08:00|         13|       15.0|            true|                      true|                      false|                                           false|
8. |1970-01-01T08:00:00.005+08:00|         26|        0.0|            true|                     false|                       true|                                            true|
9. |1970-01-01T08:00:00.008+08:00|          1|       22.0|           false|                      true|                      false|                                           false|
10. |1970-01-01T08:00:00.010+08:00|         23|       12.0|            true|                     false|                       true|                                            true|
11. +-----------------------------+-----------+-----------+----------------+--------------------------+---------------------------+------------------------------------------------+

BETWEEN ... AND ... 运算符

运算符 含义
BETWEEN … AND … 在指定范围内
NOT BETWEEN … AND … 不在指定范围内

示例: 选择区间 [36.5,40] 内或之外的数据:

1. select temperature from root.sg1.d1 where temperature between 36.5 and 40;
1. select temperature from root.sg1.d1 where temperature not between 36.5 and 40;

模糊匹配运算符

对于 TEXT 类型的数据,支持使用 Like 和 Regexp 运算符对数据进行模糊匹配

运算符 含义
LIKE 匹配简单模式
NOT LIKE 无法匹配简单模式
REGEXP 匹配正则表达式
NOT REGEXP 无法匹配正则表达式

输入数据类型:TEXT

返回类型:BOOLEAN

使用 Like 进行模糊匹配

匹配规则:

  • % 表示任意0个或多个字符。
  • _ 表示任意单个字符。

示例 1: 查询 root.sg.d1 下 value 含有'cc'的数据。

1. IoTDB> select * from root.sg.d1 where value like '%cc%'
2. +-----------------------------+----------------+
3. |                         Time|root.sg.d1.value|
4. +-----------------------------+----------------+
5. |2017-11-01T00:00:00.000+08:00|        aabbccdd|
6. |2017-11-01T00:00:01.000+08:00|              cc|
7. +-----------------------------+----------------+
8. Total line number = 2
9. It costs 0.002s

示例 2: 查询 root.sg.d1 下 value 中间为 'b'、前后为任意单个字符的数据。

1. IoTDB> select * from root.sg.device where value like '_b_'
2. +-----------------------------+----------------+
3. |                         Time|root.sg.d1.value|
4. +-----------------------------+----------------+
5. |2017-11-01T00:00:02.000+08:00|             abc|
6. +-----------------------------+----------------+
7. Total line number = 1
8. It costs 0.002s

使用 Regexp 进行模糊匹配

需要传入的过滤条件为 Java 标准库风格的正则表达式。

常见的正则匹配举例:

1. 长度为3-20的所有字符:^.{3,20}$
2. 大写英文字符:^[A-Z]+$
3. 数字和英文字符:^[A-Za-z0-9]+$
4. 以a开头的:^a.*

示例 1: 查询 root.sg.d1 下 value 值为26个英文字符组成的字符串。

1. IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$'
2. +-----------------------------+----------------+
3. |                         Time|root.sg.d1.value|
4. +-----------------------------+----------------+
5. |2017-11-01T00:00:00.000+08:00|        aabbccdd|
6. |2017-11-01T00:00:01.000+08:00|              cc|
7. +-----------------------------+----------------+
8. Total line number = 2
9. It costs 0.002s

示例 2: 查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的。

1. IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100
2. +-----------------------------+----------------+
3. |                         Time|root.sg.d1.value|
4. +-----------------------------+----------------+
5. |2017-11-01T00:00:00.000+08:00|        aabbccdd|
6. |2017-11-01T00:00:01.000+08:00|              cc|
7. +-----------------------------+----------------+
8. Total line number = 2
9. It costs 0.002s

示例 3:

1. select b, b like '1%', b regexp '[0-2]' from root.test;

运行结果

1. +-----------------------------+-----------+-------------------------+--------------------------+
2. |                         Time|root.test.b|root.test.b LIKE '^1.*?$'|root.test.b REGEXP '[0-2]'|
3. +-----------------------------+-----------+-------------------------+--------------------------+
4. |1970-01-01T08:00:00.001+08:00| 111test111|                     true|                      true|
5. |1970-01-01T08:00:00.003+08:00| 333test333|                    false|                     false|
6. +-----------------------------+-----------+-------------------------+--------------------------+

IS NULL 运算符

运算符 含义
IS NULL 是空值
IS NOT NULL 不是空值

示例 1: 选择值为空的数据:

1. select code from root.sg1.d1 where temperature is null;

示例 2: 选择值为非空的数据:

1. select code from root.sg1.d1 where temperature is not null;

IN 运算符

运算符 含义
IN / CONTAINS 是指定列表中的值
NOT IN / NOT CONTAINS 不是指定列表中的值

输入数据类型:All Types

返回类型 BOOLEAN

注意:请确保集合中的值可以被转为输入数据的类型。

例如:

s1 in (1, 2, 3, 'test'),s1的数据类型是INT32

我们将会抛出异常,因为'test'不能被转为INT32类型

示例 1: 选择值在特定范围内的数据:

1. select code from root.sg1.d1 where code in ('200', '300', '400', '500');

示例 2: 选择值在特定范围外的数据:

1. select code from root.sg1.d1 where code not in ('200', '300', '400', '500');

示例 3:

1. select a, a in (1, 2) from root.test;

输出2:

1. +-----------------------------+-----------+--------------------+
2. |                         Time|root.test.a|root.test.a IN (1,2)|
3. +-----------------------------+-----------+--------------------+
4. |1970-01-01T08:00:00.001+08:00|          1|                true|
5. |1970-01-01T08:00:00.003+08:00|          3|               false|
6. +-----------------------------+-----------+--------------------+

条件函数

条件函数针对每个数据点进行条件判断,返回布尔值。

函数名 可接收的输入序列类型 必要的属性参数 输出序列类型 功能类型
ON_OFF INT32 / INT64 / FLOAT / DOUBLE threshold:DOUBLE类型 BOOLEAN 类型 返回ts_value >= threshold的bool值
IN_RANGE INT32 / INT64 / FLOAT / DOUBLE lower:DOUBLE类型 upper:DOUBLE类型 BOOLEAN类型 返回ts_value >= lower && ts_value <= upper的bool值

测试数据:

1. IoTDB> select ts from root.test;
2. +-----------------------------+------------+
3. |                         Time|root.test.ts|
4. +-----------------------------+------------+
5. |1970-01-01T08:00:00.001+08:00|           1|
6. |1970-01-01T08:00:00.002+08:00|           2|
7. |1970-01-01T08:00:00.003+08:00|           3|
8. |1970-01-01T08:00:00.004+08:00|           4|
9. +-----------------------------+------------+

示例 1:

SQL语句:

1. select ts, on_off(ts, 'threshold'='2') from root.test;

输出:

1. IoTDB> select ts, on_off(ts, 'threshold'='2') from root.test;
2. +-----------------------------+------------+-------------------------------------+
3. |                         Time|root.test.ts|on_off(root.test.ts, "threshold"="2")|
4. +-----------------------------+------------+-------------------------------------+
5. |1970-01-01T08:00:00.001+08:00|           1|                                false|
6. |1970-01-01T08:00:00.002+08:00|           2|                                 true|
7. |1970-01-01T08:00:00.003+08:00|           3|                                 true|
8. |1970-01-01T08:00:00.004+08:00|           4|                                 true|
9. +-----------------------------+------------+-------------------------------------+

示例 2:

Sql语句:

1. select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test;

输出:

1. IoTDB> select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test;
2. +-----------------------------+------------+--------------------------------------------------+
3. |                         Time|root.test.ts|in_range(root.test.ts, "lower"="2", "upper"="3.1")|
4. +-----------------------------+------------+--------------------------------------------------+
5. |1970-01-01T08:00:00.001+08:00|           1|                                             false|
6. |1970-01-01T08:00:00.002+08:00|           2|                                              true|
7. |1970-01-01T08:00:00.003+08:00|           3|                                              true|
8. |1970-01-01T08:00:00.004+08:00|           4|                                             false|
9. +-----------------------------+------------+--------------------------------------------------+