diff --git a/src/UserGuide/Master/Table/SQL-Manual/Select-Clause.md b/src/UserGuide/Master/Table/SQL-Manual/Select-Clause.md index c50643968..5f7bf79ba 100644 --- a/src/UserGuide/Master/Table/SQL-Manual/Select-Clause.md +++ b/src/UserGuide/Master/Table/SQL-Manual/Select-Clause.md @@ -41,6 +41,7 @@ setQuantifier - It supports aggregate functions (e.g., `SUM`, `AVG`, `COUNT`) and window functions, logically executed last in the query process. - DISTINCT Keyword: `SELECT DISTINCT column_name` ensures that the values in the query results are unique, removing duplicates. +- COLUMNS Function: The COLUMNS function is supported in the SELECT clause for column filtering. It can be combined with expressions, allowing the expression's logic to apply to all columns selected by the function. ## 2. Detailed Syntax: @@ -58,6 +59,17 @@ Usage scenarios for DISTINCT: 3. **AGROUP BY Clause**: Use ALL and DISTINCT quantifiers in the GROUP BY clause to determine whether each duplicate grouping set produces distinct output rows. +`COLUMNS` Function: + +1. **`COLUMNS(*)`**: Matches all columns and supports combining with expressions. +2. **`COLUMNS(regexStr) ? AS identifier`**: Regular expression matching + - Selects columns whose names match the specified regular expression `(regexStr)` and supports combining with expressions. + - Allows renaming columns by referencing groups captured by the regular expression. If `AS` is omitted, the original column name is displayed in the format `_coln_original_name` (where `n` is the column’s position in the result table). + - Renaming Syntax: + - Use parentheses () in regexStr to define capture groups. + - Reference captured groups in identifier using `'$index'`. + - Note: The identifier must be enclosed in double quotes if it contains special characters like `$`. + ## 3. Example Data @@ -262,6 +274,116 @@ Total line number = 18 It costs 0.189s ``` +### 3.2 Colums Function + +1. Without combining expressions +```sql +-- Query data from columns whose names start with 'm' +IoTDB:database1> select columns('^m.*') from table1 limit 5 ++--------+-----------+ +|model_id|maintenance| ++--------+-----------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++--------+-----------+ + + +-- Query columns whose names start with 'o' - throw an exception if no columns match +IoTDB:database1> select columns('^o.*') from table1 limit 5 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: No matching columns found that match regex '^o.*' + + +-- Query data from columns whose names start with 'm' and rename them with 'series_' prefix +IoTDB:database1> select columns('^m(.*)') AS "series_$0" from table1 limit 5 ++---------------+------------------+ +|series_model_id|series_maintenance| ++---------------+------------------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++---------------+------------------+ +``` + +2. With Expression Combination + +- Single COLUMNS Function +```sql +-- Query the minimum value of all columns +IoTDB:database1> select min(columns(*)) from table1 ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +| _col0_time|_col1_region|_col2_plant_id|_col3_device_id|_col4_model_id|_col5_maintenance|_col6_temperature|_col7_humidity|_col8_status| _col9_arrival_time| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +|2024-11-26T13:37:00.000+08:00| 上海| 1001| 100| A| 180| 85.0| 34.8| false|2024-11-26T13:37:34.000+08:00| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +``` + +- Multiple COLUMNS Functions in Same Expression + +> Usage Restriction: When multiple COLUMNS functions appear in the same expression, their parameters must be identical. + +```sql +-- Query the sum of minimum and maximum values for columns starting with 'h' +IoTDB:database1> select min(columns('^h.*')) + max(columns('^h.*')) from table1 ++--------------+ +|_col0_humidity| ++--------------+ +| 79.899994| ++--------------+ + +-- Error Case: Non-Identical COLUMNS Functions +IoTDB:database1> select min(columns('^h.*')) + max(columns('^t.*')) from table1 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Multiple different COLUMNS in the same expression are not supported +``` + +- Multiple COLUMNS Functions in Different Expressions + +```sql +-- Query minimum of 'h'-columns and maximum of 'h'-columns separately +IoTDB:database1> select min(columns('^h.*')) , max(columns('^h.*')) from table1 ++--------------+--------------+ +|_col0_humidity|_col1_humidity| ++--------------+--------------+ +| 34.8| 45.1| ++--------------+--------------+ + +-- Query minimum of 'h'-columns and maximum of 'te'-columns +IoTDB:database1> select min(columns('^h.*')) , max(columns('^te.*')) from table1 ++--------------+-----------------+ +|_col0_humidity|_col1_temperature| ++--------------+-----------------+ +| 34.8| 90.0| ++--------------+-----------------+ +``` + +3. In Where Clause + +```sql +-- Query data where all 'h'-columns must be > 40 (equivalent to) +IoTDB:database1> select * from table1 where columns('^h.*') > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ + +--Alternative syntax +IoTDB:database1> select * from table1 where humidity > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +``` + ## 4. Column Order in the Result Set - **Column Order**: The order of columns in the result set matches the order specified in the `SELECT` clause. diff --git a/src/UserGuide/latest-Table/SQL-Manual/Select-Clause.md b/src/UserGuide/latest-Table/SQL-Manual/Select-Clause.md index c50643968..4d1b7a2a9 100644 --- a/src/UserGuide/latest-Table/SQL-Manual/Select-Clause.md +++ b/src/UserGuide/latest-Table/SQL-Manual/Select-Clause.md @@ -21,7 +21,7 @@ # SELECT Clauses -**SELECT Clause** specifies the columns included in the query results. +**SELECT Clause** specifies the columns included in the query results. ## 1. Syntax Overview @@ -41,6 +41,7 @@ setQuantifier - It supports aggregate functions (e.g., `SUM`, `AVG`, `COUNT`) and window functions, logically executed last in the query process. - DISTINCT Keyword: `SELECT DISTINCT column_name` ensures that the values in the query results are unique, removing duplicates. +- COLUMNS Function: The COLUMNS function is supported in the SELECT clause for column filtering. It can be combined with expressions, allowing the expression's logic to apply to all columns selected by the function. ## 2. Detailed Syntax: @@ -58,6 +59,17 @@ Usage scenarios for DISTINCT: 3. **AGROUP BY Clause**: Use ALL and DISTINCT quantifiers in the GROUP BY clause to determine whether each duplicate grouping set produces distinct output rows. +`COLUMNS` Function: + +1. **`COLUMNS(*)`**: Matches all columns and supports combining with expressions. +2. **`COLUMNS(regexStr) ? AS identifier`**: Regular expression matching + - Selects columns whose names match the specified regular expression `(regexStr)` and supports combining with expressions. + - Allows renaming columns by referencing groups captured by the regular expression. If `AS` is omitted, the original column name is displayed in the format `_coln_original_name` (where `n` is the column’s position in the result table). + - Renaming Syntax: + - Use parentheses () in regexStr to define capture groups. + - Reference captured groups in identifier using `'$index'`. + - Note: The identifier must be enclosed in double quotes if it contains special characters like `$`. + ## 3. Example Data @@ -262,6 +274,116 @@ Total line number = 18 It costs 0.189s ``` +### 3.2 Colums Function + +1. Without combining expressions +```sql +-- Query data from columns whose names start with 'm' +IoTDB:database1> select columns('^m.*') from table1 limit 5 ++--------+-----------+ +|model_id|maintenance| ++--------+-----------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++--------+-----------+ + + +-- Query columns whose names start with 'o' - throw an exception if no columns match +IoTDB:database1> select columns('^o.*') from table1 limit 5 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: No matching columns found that match regex '^o.*' + + +-- Query data from columns whose names start with 'm' and rename them with 'series_' prefix +IoTDB:database1> select columns('^m(.*)') AS "series_$0" from table1 limit 5 ++---------------+------------------+ +|series_model_id|series_maintenance| ++---------------+------------------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++---------------+------------------+ +``` + +2. With Expression Combination + +- Single COLUMNS Function +```sql +-- Query the minimum value of all columns +IoTDB:database1> select min(columns(*)) from table1 ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +| _col0_time|_col1_region|_col2_plant_id|_col3_device_id|_col4_model_id|_col5_maintenance|_col6_temperature|_col7_humidity|_col8_status| _col9_arrival_time| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +|2024-11-26T13:37:00.000+08:00| 上海| 1001| 100| A| 180| 85.0| 34.8| false|2024-11-26T13:37:34.000+08:00| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +``` + +- Multiple COLUMNS Functions in Same Expression + +> Usage Restriction: When multiple COLUMNS functions appear in the same expression, their parameters must be identical. + +```sql +-- Query the sum of minimum and maximum values for columns starting with 'h' +IoTDB:database1> select min(columns('^h.*')) + max(columns('^h.*')) from table1 ++--------------+ +|_col0_humidity| ++--------------+ +| 79.899994| ++--------------+ + +-- Error Case: Non-Identical COLUMNS Functions +IoTDB:database1> select min(columns('^h.*')) + max(columns('^t.*')) from table1 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Multiple different COLUMNS in the same expression are not supported +``` + +- Multiple COLUMNS Functions in Different Expressions + +```sql +-- Query minimum of 'h'-columns and maximum of 'h'-columns separately +IoTDB:database1> select min(columns('^h.*')) , max(columns('^h.*')) from table1 ++--------------+--------------+ +|_col0_humidity|_col1_humidity| ++--------------+--------------+ +| 34.8| 45.1| ++--------------+--------------+ + +-- Query minimum of 'h'-columns and maximum of 'te'-columns +IoTDB:database1> select min(columns('^h.*')) , max(columns('^te.*')) from table1 ++--------------+-----------------+ +|_col0_humidity|_col1_temperature| ++--------------+-----------------+ +| 34.8| 90.0| ++--------------+-----------------+ +``` + +3. In Where Clause + +```sql +-- Query data where all 'h'-columns must be > 40 (equivalent to) +IoTDB:database1> select * from table1 where columns('^h.*') > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ + +--Alternative syntax +IoTDB:database1> select * from table1 where humidity > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +``` + ## 4. Column Order in the Result Set - **Column Order**: The order of columns in the result set matches the order specified in the `SELECT` clause. diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/Select-Clause.md b/src/zh/UserGuide/Master/Table/SQL-Manual/Select-Clause.md index d0a92c3a3..2631a0f57 100644 --- a/src/zh/UserGuide/Master/Table/SQL-Manual/Select-Clause.md +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/Select-Clause.md @@ -39,6 +39,7 @@ setQuantifier - **SELECT 子句**: 指定了查询结果应包含的列,包含聚合函数(如 SUM、AVG、COUNT 等)以及窗口函数,在逻辑上最后执行。 - **DISTINCT 关键字**: `SELECT DISTINCT column_name` 确保查询结果中的值是唯一的,去除重复项。 +- **COLUMNS 函数**:SELECT 子句中支持使用 COLUMNS 函数进行列筛选,并支持和表达式结合使用,使表达式的效果对所有筛选出的列生效。 ## 2. 语法详释: @@ -54,6 +55,17 @@ setQuantifier - **聚合函数**:与聚合函数一起使用时,DISTINCT 只处理输入数据集中的非重复行。 - **GROUP BY 子句**:在 GROUP BY 子句中使用 ALL 和 DISTINCT 量词,决定是否每个重复的分组集产生不同的输出行。 +`COLUMNS` 函数: +- **`COLUMNS(*)`**: 匹配所有列,支持结合表达式进行使用。 +- **`COLUMNS(regexStr) ? AS identifier`**:正则匹配 + - 匹配所有列名满足正则表达式的列,支持结合表达式进行使用。 + - 支持引用正则表达式捕获到的 groups 对列进行重命名,不写 AS 时展示原始列名(即 _coln_原始列名,其中 n 为列在结果表中的 position)。 + - 重命名用法简述: + - regexStr 中使用圆括号设置要捕获的组; + - 在 identifier 中使用 `'$index'` 引用捕获到的组。 + + 注意:使用该功能时,identifier 中会包含特殊字符 '$',所以整个 identifier 要用双引号引起来。 + ## 3. 示例数据 在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 @@ -255,6 +267,116 @@ Total line number = 18 It costs 0.189s ``` +### 3.2 Colums 函数 + +1. 不结合表达式 +```sql +-- 查询列名以 'm' 开头的列的数据 +IoTDB:database1> select columns('^m.*') from table1 limit 5 ++--------+-----------+ +|model_id|maintenance| ++--------+-----------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++--------+-----------+ + + +-- 查询列名以 'o' 开头的列,未匹配到任何列,抛出异常 +IoTDB:database1> select columns('^o.*') from table1 limit 5 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: No matching columns found that match regex '^o.*' + + +-- 查询列名以 'm' 开头的列的数据,并重命名以 'series_' 开头 +IoTDB:database1> select columns('^m(.*)') AS "series_$0" from table1 limit 5 ++---------------+------------------+ +|series_model_id|series_maintenance| ++---------------+------------------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++---------------+------------------+ +``` + +2. 结合表达式 + +- 单个 COLUMNS 函数 +```sql +-- 查询所有列的最小值 +IoTDB:database1> select min(columns(*)) from table1 ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +| _col0_time|_col1_region|_col2_plant_id|_col3_device_id|_col4_model_id|_col5_maintenance|_col6_temperature|_col7_humidity|_col8_status| _col9_arrival_time| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +|2024-11-26T13:37:00.000+08:00| 上海| 1001| 100| A| 180| 85.0| 34.8| false|2024-11-26T13:37:34.000+08:00| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +``` + +- 多个 COLUMNS 函数,出现在同一表达式 + +> 使用限制:出现多个 COLUMNS 函数时,多个 COLUMNS 函数的参数要完全相同 + +```sql +-- 查询 'h' 开头列的最小值和最大值之和 +IoTDB:database1> select min(columns('^h.*')) + max(columns('^h.*')) from table1 ++--------------+ +|_col0_humidity| ++--------------+ +| 79.899994| ++--------------+ + +-- 错误查询,两个 COLUMNS 函数不完全相同 +IoTDB:database1> select min(columns('^h.*')) + max(columns('^t.*')) from table1 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Multiple different COLUMNS in the same expression are not supported +``` + +- 多个 COLUMNS 函数,出现在不同表达式 + +```sql +-- 分别查询 'h' 开头列的最小值和最大值 +IoTDB:database1> select min(columns('^h.*')) , max(columns('^h.*')) from table1 ++--------------+--------------+ +|_col0_humidity|_col1_humidity| ++--------------+--------------+ +| 34.8| 45.1| ++--------------+--------------+ + +-- 分别查询 'h' 开头列的最小值和 'te'开头列的最大值 +IoTDB:database1> select min(columns('^h.*')) , max(columns('^te.*')) from table1 ++--------------+-----------------+ +|_col0_humidity|_col1_temperature| ++--------------+-----------------+ +| 34.8| 90.0| ++--------------+-----------------+ +``` + +3. 在 WHERE 子句中使用 + +```sql +-- 查询数据,所有 'h' 开头列的数据必须要大于 40 +IoTDB:database1> select * from table1 where columns('^h.*') > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ + +--等价于 +IoTDB:database1> select * from table1 where humidity > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +``` + ## 4. 结果集列顺序 - **列顺序**: 结果集中的列顺序与 SELECT 子句中指定的顺序相同。 diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/Select-Clause.md b/src/zh/UserGuide/latest-Table/SQL-Manual/Select-Clause.md index d0a92c3a3..223157003 100644 --- a/src/zh/UserGuide/latest-Table/SQL-Manual/Select-Clause.md +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/Select-Clause.md @@ -38,7 +38,8 @@ setQuantifier ``` - **SELECT 子句**: 指定了查询结果应包含的列,包含聚合函数(如 SUM、AVG、COUNT 等)以及窗口函数,在逻辑上最后执行。 -- **DISTINCT 关键字**: `SELECT DISTINCT column_name` 确保查询结果中的值是唯一的,去除重复项。 +- **DISTINCT 关键字**: `SELECT DISTINCT column_name` 确保查询结果中的值是唯一的,去除重复项。 +- **COLUMNS 函数**:SELECT 子句中支持使用 COLUMNS 函数进行列筛选,并支持和表达式结合使用,使表达式的效果对所有筛选出的列生效。 ## 2. 语法详释: @@ -54,6 +55,17 @@ setQuantifier - **聚合函数**:与聚合函数一起使用时,DISTINCT 只处理输入数据集中的非重复行。 - **GROUP BY 子句**:在 GROUP BY 子句中使用 ALL 和 DISTINCT 量词,决定是否每个重复的分组集产生不同的输出行。 +`COLUMNS` 函数: +- **`COLUMNS(*)`**: 匹配所有列,支持结合表达式进行使用。 +- **`COLUMNS(regexStr) ? AS identifier`**:正则匹配 + - 匹配所有列名满足正则表达式的列,支持结合表达式进行使用。 + - 支持引用正则表达式捕获到的 groups 对列进行重命名,不写 AS 时展示原始列名(即 _coln_原始列名,其中 n 为列在结果表中的 position)。 + - 重命名用法简述: + - regexStr 中使用圆括号设置要捕获的组; + - 在 identifier 中使用 `'$index'` 引用捕获到的组。 + + 注意:使用该功能时,identifier 中会包含特殊字符 '$',所以整个 identifier 要用双引号引起来。 + ## 3. 示例数据 在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 @@ -255,6 +267,116 @@ Total line number = 18 It costs 0.189s ``` +### 3.2 Colums 函数 + +1. 不结合表达式 +```sql +-- 查询列名以 'm' 开头的列的数据 +IoTDB:database1> select columns('^m.*') from table1 limit 5 ++--------+-----------+ +|model_id|maintenance| ++--------+-----------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++--------+-----------+ + + +-- 查询列名以 'o' 开头的列,未匹配到任何列,抛出异常 +IoTDB:database1> select columns('^o.*') from table1 limit 5 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: No matching columns found that match regex '^o.*' + + +-- 查询列名以 'm' 开头的列的数据,并重命名以 'series_' 开头 +IoTDB:database1> select columns('^m(.*)') AS "series_$0" from table1 limit 5 ++---------------+------------------+ +|series_model_id|series_maintenance| ++---------------+------------------+ +| E| 180| +| E| 180| +| C| 90| +| C| 90| +| C| 90| ++---------------+------------------+ +``` + +2. 结合表达式 + +- 单个 COLUMNS 函数 +```sql +-- 查询所有列的最小值 +IoTDB:database1> select min(columns(*)) from table1 ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +| _col0_time|_col1_region|_col2_plant_id|_col3_device_id|_col4_model_id|_col5_maintenance|_col6_temperature|_col7_humidity|_col8_status| _col9_arrival_time| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +|2024-11-26T13:37:00.000+08:00| 上海| 1001| 100| A| 180| 85.0| 34.8| false|2024-11-26T13:37:34.000+08:00| ++-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+ +``` + +- 多个 COLUMNS 函数,出现在同一表达式 + +> 使用限制:出现多个 COLUMNS 函数时,多个 COLUMNS 函数的参数要完全相同 + +```sql +-- 查询 'h' 开头列的最小值和最大值之和 +IoTDB:database1> select min(columns('^h.*')) + max(columns('^h.*')) from table1 ++--------------+ +|_col0_humidity| ++--------------+ +| 79.899994| ++--------------+ + +-- 错误查询,两个 COLUMNS 函数不完全相同 +IoTDB:database1> select min(columns('^h.*')) + max(columns('^t.*')) from table1 +Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Multiple different COLUMNS in the same expression are not supported +``` + +- 多个 COLUMNS 函数,出现在不同表达式 + +```sql +-- 分别查询 'h' 开头列的最小值和最大值 +IoTDB:database1> select min(columns('^h.*')) , max(columns('^h.*')) from table1 ++--------------+--------------+ +|_col0_humidity|_col1_humidity| ++--------------+--------------+ +| 34.8| 45.1| ++--------------+--------------+ + +-- 分别查询 'h' 开头列的最小值和 'te'开头列的最大值 +IoTDB:database1> select min(columns('^h.*')) , max(columns('^te.*')) from table1 ++--------------+-----------------+ +|_col0_humidity|_col1_temperature| ++--------------+-----------------+ +| 34.8| 90.0| ++--------------+-----------------+ +``` + +3. 在 WHERE 子句中使用 + +```sql +-- 查询数据,所有 'h' 开头列的数据必须要大于 40 +IoTDB:database1> select * from table1 where columns('^h.*') > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ + +--等价于 +IoTDB:database1> select * from table1 where humidity > 40 ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null| +|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +``` + ## 4. 结果集列顺序 - **列顺序**: 结果集中的列顺序与 SELECT 子句中指定的顺序相同。