diff --git a/src/.vuepress/public/img/data-query-1.png b/src/.vuepress/public/img/data-query-1.png new file mode 100644 index 000000000..eba3d647b Binary files /dev/null and b/src/.vuepress/public/img/data-query-1.png differ diff --git a/src/.vuepress/public/img/window-function-1.png b/src/.vuepress/public/img/window-function-1.png new file mode 100644 index 000000000..eac94edc7 Binary files /dev/null and b/src/.vuepress/public/img/window-function-1.png differ diff --git a/src/.vuepress/sidebar/V2.0.x/en-Table.ts b/src/.vuepress/sidebar/V2.0.x/en-Table.ts index 630f48597..6d52d580f 100644 --- a/src/.vuepress/sidebar/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/en-Table.ts @@ -77,7 +77,7 @@ export const enSidebar = { { text: 'Database Management', link: 'Database-Management' }, { text: 'Table Management', link: 'Table-Management' }, { text: 'Write&Updata Data', link: 'Write-Updata-Data' }, - { text: 'Query Data', link: 'Query-Data' }, + { text: 'Query Data', link: 'Query-Data_apache' }, { text: 'Delete Data', collapsible: true, @@ -202,7 +202,7 @@ export const enSidebar = { text: 'Data Query', collapsible: true, children: [ - { text: 'overview', link: 'overview' }, + { text: 'overview', link: 'overview_apache' }, { text: 'SELECT Clause', link: 'Select-Clause' }, { text: 'FROM&JOIN Clause', link: 'From-Join-Clause' }, { text: 'WHERE Clause', link: 'Where-Clause' }, diff --git a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts index 48c58a8d5..67875b006 100644 --- a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts @@ -70,7 +70,7 @@ export const zhSidebar = { { text: '数据库管理', link: 'Database-Management' }, { text: '表管理', link: 'Table-Management' }, { text: '写入&更新', link: 'Write-Updata-Data' }, - { text: '数据查询', link: 'Query-Data' }, + { text: '数据查询', link: 'Query-Data_apache' }, { text: '数据删除', collapsible: true, @@ -192,7 +192,7 @@ export const zhSidebar = { text: '数据查询', collapsible: true, children: [ - { text: '概览', link: 'overview' }, + { text: '概览', link: 'overview_apache' }, { text: 'SELECT子句', link: 'Select-Clause' }, { text: 'FROM&JOIN子句', link: 'From-Join-Clause' }, { text: 'WHERE子句', link: 'Where-Clause' }, diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts index 8dccc2b19..c18c42361 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts @@ -81,7 +81,7 @@ export const enSidebar = { { text: 'Database Management', link: 'Database-Management' }, { text: 'Table Management', link: 'Table-Management' }, { text: 'Write&Updata Data', link: 'Write-Updata-Data' }, - { text: 'Query Data', link: 'Query-Data' }, + { text: 'Query Data', link: 'Query-Data_timecho' }, { text: 'Delete Data', collapsible: true, @@ -207,7 +207,7 @@ export const enSidebar = { text: 'Data Query', collapsible: true, children: [ - { text: 'overview', link: 'overview' }, + { text: 'overview', link: 'overview_timecho' }, { text: 'SELECT Clause', link: 'Select-Clause' }, { text: 'FROM&JOIN Clause', link: 'From-Join-Clause' }, { text: 'WHERE Clause', link: 'Where-Clause' }, diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts index 5b22eb6d2..f5e8f90c6 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts @@ -72,7 +72,7 @@ export const zhSidebar = { { text: '数据库管理', link: 'Database-Management' }, { text: '表管理', link: 'Table-Management' }, { text: '写入&更新', link: 'Write-Updata-Data' }, - { text: '数据查询', link: 'Query-Data' }, + { text: '数据查询', link: 'Query-Data_timecho' }, { text: '数据删除', collapsible: true, @@ -196,7 +196,7 @@ export const zhSidebar = { text: '数据查询', collapsible: true, children: [ - { text: '概览', link: 'overview' }, + { text: '概览', link: 'overview_timecho' }, { text: 'SELECT子句', link: 'Select-Clause' }, { text: 'FROM&JOIN子句', link: 'From-Join-Clause' }, { text: 'WHERE子句', link: 'Where-Clause' }, diff --git a/src/UserGuide/Master/Table/Basic-Concept/Query-Data.md b/src/UserGuide/Master/Table/Basic-Concept/Query-Data.md index 22ec4c071..fd9f42512 100644 --- a/src/UserGuide/Master/Table/Basic-Concept/Query-Data.md +++ b/src/UserGuide/Master/Table/Basic-Concept/Query-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Query-Data_apache.html +--- - -# Query Data - -## 1. Syntax Overview - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -The IoTDB table model query syntax supports the following clauses: - -- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) -- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) -- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) -- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) -- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) -- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) -- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) -- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) -- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. Clause Execution Order - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) - - -## 3. Common Query Examples - -### 3.1 Sample Dataset - -The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. - -### 3.2 Basic Data Query - -**Example 1: Filter by Time** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T08:00:00.000+08:00| 85.0| null| -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 11 -It costs 0.075s -``` - -**Example 2: Filter by** **Value** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE temperature > 89.0; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 5 -It costs 0.156s -``` - -**Example 3: Filter by Attribute** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE model_id ='B'; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.106s -``` -**Example 3:Multi device time aligned query** - -```SQL -IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, - device_id, - AVG(temperature) AS avg_temp - FROM table1 - WHERE TIME >= 2024-11-26 13:00:00 - AND TIME <= 2024-11-27 17:00:00 - GROUP BY 1, device_id FILL METHOD PREVIOUS; -``` - -**Result**: - -```SQL -+-----------------------------+---------+--------+ -| a_time|device_id|avg_temp| -+-----------------------------+---------+--------+ -|2024-11-26T08:00:00.000+08:00| 100| 90.0| -|2024-11-27T08:00:00.000+08:00| 100| 90.0| -|2024-11-26T08:00:00.000+08:00| 101| 90.0| -|2024-11-27T08:00:00.000+08:00| 101| 85.0| -+-----------------------------+---------+--------+ -Total line number = 4 -It costs 0.048s -``` - -### 3.3 Aggregation Query - -**Example**: Calculate the average, maximum, and minimum temperature for each `device_id` within a specific time range. - -```SQL -IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 - GROUP BY device_id; -``` - -**Result**: - -```SQL -+---------+--------+--------+--------+ -|device_id|avg_temp|max_temp|min_temp| -+---------+--------+--------+--------+ -| 100| 87.6| 90.0| 85.0| -| 101| 85.0| 85.0| 85.0| -+---------+--------+--------+--------+ -Total line number = 2 -It costs 0.278s -``` - -### 3.4 Latest Point Query - -**Example**: Retrieve the latest record for each `device_id`, including the temperature value and the timestamp of the last record. - -```SQL -IoTDB> SELECT device_id,last(time),last_by(temperature,time) - FROM table1 - GROUP BY device_id; -``` - -**Result**: - -```SQL -+---------+-----------------------------+-----+ -|device_id| _col1|_col2| -+---------+-----------------------------+-----+ -| 100|2024-11-29T18:30:00.000+08:00| 90.0| -| 101|2024-11-30T14:30:00.000+08:00| 90.0| -+---------+-----------------------------+-----+ -Total line number = 2 -It costs 0.090s -``` - -### 3.5 Downsampling Query - -**Example**: Group data by day and calculate the average temperature using `date_bin_gapfill` function. - -```SQL -IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 - GROUP BY device_id,date_bin(1d ,time); -``` - -**Result**: - -```SQL -+---------+-----------------------------+--------+ -|device_id| day_time|avg_temp| -+---------+-----------------------------+--------+ -| 100|2024-11-29T08:00:00.000+08:00| 90.0| -| 100|2024-11-28T08:00:00.000+08:00| 86.0| -| 100|2024-11-26T08:00:00.000+08:00| 90.0| -| 101|2024-11-29T08:00:00.000+08:00| 85.0| -| 101|2024-11-27T08:00:00.000+08:00| 85.0| -+---------+-----------------------------+--------+ -Total line number = 5 -It costs 0.066s -``` -### 3.6 Multi device downsampling alignment query - -#### 3.6.1 Sampling Frequency is the Same, but Time is Different - -**Table 1: Sampling Frequency: 1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**Table 2: Sampling Frequency: 1s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.003 | d1 | 35.1 | -| 00:00:01.012 | d1 | 37.2 | -| 00:00:02.031 | d1 | null | -| 00:00:03.134 | d1 | 35.2 | -| 00:00:04.201 | d1 | 38.2 | -| 00:00:05.091 | d1 | 35.4 | -| 00:00:06.231 | d1 | 35.1 | - -**Example: Querying the downsampled data of table1:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` - -**Example: Querying the downsampled data of table2:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| b_time|b_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 37.2| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 35.2| -|2025-05-13T00:00:04.000+08:00| 38.2| -|2025-05-13T00:00:05.000+08:00| 35.4| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**Example: Aligning multiple sequences by integer time:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) B - USING (time) -``` - -**Result:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -- **Retaining NULL Values**: When NULL values have special significance or when you wish to preserve the null values in the data, you can choose to omit FILL METHOD PREVIOUS to avoid filling in the gaps. -**Example:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) B - USING (time) -``` - -**Result:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| null| -|2025-05-13T00:00:03.000+08:00| null| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` -#### 3.6.2 Different Sampling Frequencies, Different Times - -**Table 1: Sampling Frequency: 1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**Table 3: Sampling Frequency: 2s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.005 | d1 | 35.1 | -| 00:00:02.106 | d1 | 37.2 | -| 00:00:04.187 | d1 | null | -| 00:00:06.156 | d1 | 35.1 | - -**Example: Querying the downsampled data of table1:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` -**Example: Querying the downsampled data of table3:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| c_time|c_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 35.1| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 37.2| -|2025-05-13T00:00:04.000+08:00| 37.2| -|2025-05-13T00:00:05.000+08:00| 37.2| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**Example: Aligning multiple sequences by the higher sampling frequency:** - -```SQL -IoTDB> SELECT time, - a_value, - c_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) C - USING (time) -``` - -**Result:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|c_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -### 3.7 Missing Data Filling - -**Example**: Query the records within a specified time range where `device_id` is '100'. If there are missing data points, fill them using the previous non-null value. - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 - AND region='East' AND plant_id='1001' AND device_id='101' - FILL METHOD PREVIOUS; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.101s -``` - -### 3.8 Sorting & Pagination - -**Example**: Query records from the table, sorting by `humidity` in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows. - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - ORDER BY humidity desc NULLS LAST - OFFSET 2 - LIMIT 10; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 10 -It costs 0.093s -``` diff --git a/src/UserGuide/Master/Table/Basic-Concept/Query-Data_apache.md b/src/UserGuide/Master/Table/Basic-Concept/Query-Data_apache.md new file mode 100644 index 000000000..b3018d2f5 --- /dev/null +++ b/src/UserGuide/Master/Table/Basic-Concept/Query-Data_apache.md @@ -0,0 +1,589 @@ + + +# Query Data + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) + + +## 3. Common Query Examples + +### 3.1 Sample Dataset + +The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. + +### 3.2 Basic Data Query + +**Example 1: Filter by Time** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**Example 2: Filter by** **Value** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**Example 3: Filter by Attribute** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**Example 3:Multi device time aligned query** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 Aggregation Query + +**Example**: Calculate the average, maximum, and minimum temperature for each `device_id` within a specific time range. + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 Latest Point Query + +**Example**: Retrieve the latest record for each `device_id`, including the temperature value and the timestamp of the last record. + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 Downsampling Query + +**Example**: Group data by day and calculate the average temperature using `date_bin_gapfill` function. + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +**Result**: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` +### 3.6 Multi device downsampling alignment query + +#### 3.6.1 Sampling Frequency is the Same, but Time is Different + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 2: Sampling Frequency: 1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**Example: Querying the downsampled data of table2:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by integer time:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- **Retaining NULL Values**: When NULL values have special significance or when you wish to preserve the null values in the data, you can choose to omit FILL METHOD PREVIOUS to avoid filling in the gaps. +**Example:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 Different Sampling Frequencies, Different Times + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 3: Sampling Frequency: 2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**Example: Querying the downsampled data of table3:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by the higher sampling frequency:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 Missing Data Filling + +**Example**: Query the records within a specified time range where `device_id` is '100'. If there are missing data points, fill them using the previous non-null value. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='East' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 Sorting & Pagination + +**Example**: Query records from the table, sorting by `humidity` in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` diff --git a/src/UserGuide/Master/Table/Basic-Concept/Query-Data_timecho.md b/src/UserGuide/Master/Table/Basic-Concept/Query-Data_timecho.md new file mode 100644 index 000000000..659cbfd73 --- /dev/null +++ b/src/UserGuide/Master/Table/Basic-Concept/Query-Data_timecho.md @@ -0,0 +1,666 @@ + + +# Query Data + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **patternRecognition**: Row Pattern Recognition, which supports capturing a segment of continuous data by defining recognition logic for pattern variables and regular expressions, and performs analysis and calculation on each captured data segment. Details:[Row Pattern Recognition](../SQL-Manual/Row-Pattern-Recognition.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **WINDOW FUNCTION**: Window Function, a special function that performs calculations on each row based on a specific set of rows related to the current row (called a "window"). It combines grouping operations, sorting, and definable calculation ranges to implement complex cross-row calculations without collapsing the original data rows. Details: [Window Function](../SQL-Manual/Featured-Functions_timecho.md#_4-Window-Function) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/data-query-1.png) + + +## 3. Common Query Examples + +### 3.1 Sample Dataset + +The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. + +### 3.2 Basic Data Query + +**Example 1: Filter by Time** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**Example 2: Filter by** **Value** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**Example 3: Filter by Attribute** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**Example 3:Multi device time aligned query** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 Aggregation Query + +**Example**: Calculate the average, maximum, and minimum temperature for each `device_id` within a specific time range. + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 Latest Point Query + +**Example**: Retrieve the latest record for each `device_id`, including the temperature value and the timestamp of the last record. + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 Downsampling Query + +**Example**: Group data by day and calculate the average temperature using `date_bin_gapfill` function. + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +**Result**: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` +### 3.6 Multi device downsampling alignment query + +#### 3.6.1 Sampling Frequency is the Same, but Time is Different + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 2: Sampling Frequency: 1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**Example: Querying the downsampled data of table2:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by integer time:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- **Retaining NULL Values**: When NULL values have special significance or when you wish to preserve the null values in the data, you can choose to omit FILL METHOD PREVIOUS to avoid filling in the gaps. +**Example:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 Different Sampling Frequencies, Different Times + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 3: Sampling Frequency: 2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**Example: Querying the downsampled data of table3:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by the higher sampling frequency:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 Missing Data Filling + +**Example**: Query the records within a specified time range where `device_id` is '100'. If there are missing data points, fill them using the previous non-null value. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='East' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 Sorting & Pagination + +**Example**: Query records from the table, sorting by `humidity` in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` + +### 3.9 Row Pattern Recognition + +**Example**: Segment data in table1 by time intervals of 24 hours or less, and query the total number of data entries in each segment, as well as the start and end times. + +```SQL +SELECT start_time, end_time, cnt +FROM table1 +MATCH_RECOGNIZE ( + ORDER BY time + MEASURES + RPR_FIRST(A.time) AS start_time, + RPR_LAST(time) AS end_time, + COUNT() AS cnt + PATTERN (A B*) + DEFINE B AS (cast(B.time as INT64) - cast(PREV(B.time) as INT64)) <= 86400000 +) AS m +``` + +**Result**: + +```SQL ++-----------------------------+-----------------------------+---+ +| start_time| end_time|cnt| ++-----------------------------+-----------------------------+---+ +|2024-11-26T13:37:00.000+08:00|2024-11-26T13:38:00.000+08:00| 2| +|2024-11-27T16:38:00.000+08:00|2024-11-30T14:30:00.000+08:00| 16| ++-----------------------------+-----------------------------+---+ +Total line number = 2 +``` + +### 3.10 Window Functions + +**Example**: Query the cumulative power consumption values of different devices. + +The original data is as follows: + +```SQL ++-----------------------------+------+-----+ +| time|device| flow| ++-----------------------------+------+-----+ +|1970-01-01T08:00:00.000+08:00| d0| 3| +|1970-01-01T08:00:00.001+08:00| d0| 5| +|1970-01-01T08:00:00.002+08:00| d0| 3| +|1970-01-01T08:00:00.003+08:00| d0| 1| +|1970-01-01T08:00:00.004+08:00| d1| 2| +|1970-01-01T08:00:00.005+08:00| d1| 4| ++-----------------------------+------+-----+ +``` + +**Query Statement**: + +```SQL +IoTDB> SELECT *, sum(flow) OVER(PARTITION BY device ORDER BY flow) as sum FROM device_flow; +``` + +After grouping, sorting, and calculation (steps are disassembled as shown in the figure below), + +![](/img/window-function-1.png) + +**Result**: + +```SQL ++-----------------------------+------+----+----+ +| time|device|flow| sum| ++-----------------------------+------+----+----+ +|1970-01-01T08:00:04.000+08:00| d1| 2| 2.0| +|1970-01-01T08:00:05.000+08:00| d1| 4| 6.0| +|1970-01-01T08:00:03.000+08:00| d0| 1| 1.0| +|1970-01-01T08:00:00.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:02.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:01.000+08:00| d0| 5|12.0| ++-----------------------------+------+----+----+ +``` diff --git a/src/UserGuide/Master/Table/SQL-Manual/overview.md b/src/UserGuide/Master/Table/SQL-Manual/overview.md index 0ef5cbc2e..28ad594b5 100644 --- a/src/UserGuide/Master/Table/SQL-Manual/overview.md +++ b/src/UserGuide/Master/Table/SQL-Manual/overview.md @@ -1,3 +1,6 @@ +--- +redirectTo: overview_apache.html +--- - -# Overview - -## 1. Syntax Overview - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -The IoTDB table model query syntax supports the following clauses: - -- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) -- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) -- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) -- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) -- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) -- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) -- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) -- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) -- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. Clause Execution Order - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) \ No newline at end of file diff --git a/src/UserGuide/Master/Table/SQL-Manual/overview_apache.md b/src/UserGuide/Master/Table/SQL-Manual/overview_apache.md new file mode 100644 index 000000000..0ef5cbc2e --- /dev/null +++ b/src/UserGuide/Master/Table/SQL-Manual/overview_apache.md @@ -0,0 +1,52 @@ + + +# Overview + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) \ No newline at end of file diff --git a/src/UserGuide/Master/Table/SQL-Manual/overview_timecho.md b/src/UserGuide/Master/Table/SQL-Manual/overview_timecho.md new file mode 100644 index 000000000..a7be3102b --- /dev/null +++ b/src/UserGuide/Master/Table/SQL-Manual/overview_timecho.md @@ -0,0 +1,55 @@ + + +# Overview + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **patternRecognition**: Row Pattern Recognition, which supports capturing a segment of continuous data by defining recognition logic for pattern variables and regular expressions, and performs analysis and calculation on each captured data segment. Details:[Row Pattern Recognition](../SQL-Manual/Row-Pattern-Recognition.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **WINDOW FUNCTION**: Window Function, a special function that performs calculations on each row based on a specific set of rows related to the current row (called a "window"). It combines grouping operations, sorting, and definable calculation ranges to implement complex cross-row calculations without collapsing the original data rows. Details: [Window Function](../SQL-Manual/Featured-Functions_timecho.md#_4-Window-Function) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/data-query-1.png) \ No newline at end of file diff --git a/src/UserGuide/latest-Table/Basic-Concept/Query-Data.md b/src/UserGuide/latest-Table/Basic-Concept/Query-Data.md index b3018d2f5..fd9f42512 100644 --- a/src/UserGuide/latest-Table/Basic-Concept/Query-Data.md +++ b/src/UserGuide/latest-Table/Basic-Concept/Query-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Query-Data_apache.html +--- - -# Query Data - -## 1. Syntax Overview - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -The IoTDB table model query syntax supports the following clauses: - -- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) -- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) -- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) -- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) -- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) -- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) -- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) -- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) -- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. Clause Execution Order - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) - - -## 3. Common Query Examples - -### 3.1 Sample Dataset - -The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. - -### 3.2 Basic Data Query - -**Example 1: Filter by Time** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T08:00:00.000+08:00| 85.0| null| -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 11 -It costs 0.075s -``` - -**Example 2: Filter by** **Value** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE temperature > 89.0; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 5 -It costs 0.156s -``` - -**Example 3: Filter by Attribute** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE model_id ='B'; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.106s -``` - -**Example 3:Multi device time aligned query** - -```SQL -IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, - device_id, - AVG(temperature) AS avg_temp - FROM table1 - WHERE TIME >= 2024-11-26 13:00:00 - AND TIME <= 2024-11-27 17:00:00 - GROUP BY 1, device_id FILL METHOD PREVIOUS; -``` - -**Result**: - -```SQL -+-----------------------------+---------+--------+ -| a_time|device_id|avg_temp| -+-----------------------------+---------+--------+ -|2024-11-26T08:00:00.000+08:00| 100| 90.0| -|2024-11-27T08:00:00.000+08:00| 100| 90.0| -|2024-11-26T08:00:00.000+08:00| 101| 90.0| -|2024-11-27T08:00:00.000+08:00| 101| 85.0| -+-----------------------------+---------+--------+ -Total line number = 4 -It costs 0.048s -``` - -### 3.3 Aggregation Query - -**Example**: Calculate the average, maximum, and minimum temperature for each `device_id` within a specific time range. - -```SQL -IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 - GROUP BY device_id; -``` - -**Result**: - -```SQL -+---------+--------+--------+--------+ -|device_id|avg_temp|max_temp|min_temp| -+---------+--------+--------+--------+ -| 100| 87.6| 90.0| 85.0| -| 101| 85.0| 85.0| 85.0| -+---------+--------+--------+--------+ -Total line number = 2 -It costs 0.278s -``` - -### 3.4 Latest Point Query - -**Example**: Retrieve the latest record for each `device_id`, including the temperature value and the timestamp of the last record. - -```SQL -IoTDB> SELECT device_id,last(time),last_by(temperature,time) - FROM table1 - GROUP BY device_id; -``` - -**Result**: - -```SQL -+---------+-----------------------------+-----+ -|device_id| _col1|_col2| -+---------+-----------------------------+-----+ -| 100|2024-11-29T18:30:00.000+08:00| 90.0| -| 101|2024-11-30T14:30:00.000+08:00| 90.0| -+---------+-----------------------------+-----+ -Total line number = 2 -It costs 0.090s -``` - -### 3.5 Downsampling Query - -**Example**: Group data by day and calculate the average temperature using `date_bin_gapfill` function. - -```SQL -IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 - GROUP BY device_id,date_bin(1d ,time); -``` - -**Result**: - -```SQL -+---------+-----------------------------+--------+ -|device_id| day_time|avg_temp| -+---------+-----------------------------+--------+ -| 100|2024-11-29T08:00:00.000+08:00| 90.0| -| 100|2024-11-28T08:00:00.000+08:00| 86.0| -| 100|2024-11-26T08:00:00.000+08:00| 90.0| -| 101|2024-11-29T08:00:00.000+08:00| 85.0| -| 101|2024-11-27T08:00:00.000+08:00| 85.0| -+---------+-----------------------------+--------+ -Total line number = 5 -It costs 0.066s -``` -### 3.6 Multi device downsampling alignment query - -#### 3.6.1 Sampling Frequency is the Same, but Time is Different - -**Table 1: Sampling Frequency: 1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**Table 2: Sampling Frequency: 1s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.003 | d1 | 35.1 | -| 00:00:01.012 | d1 | 37.2 | -| 00:00:02.031 | d1 | null | -| 00:00:03.134 | d1 | 35.2 | -| 00:00:04.201 | d1 | 38.2 | -| 00:00:05.091 | d1 | 35.4 | -| 00:00:06.231 | d1 | 35.1 | - -**Example: Querying the downsampled data of table1:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` - -**Example: Querying the downsampled data of table2:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| b_time|b_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 37.2| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 35.2| -|2025-05-13T00:00:04.000+08:00| 38.2| -|2025-05-13T00:00:05.000+08:00| 35.4| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**Example: Aligning multiple sequences by integer time:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) B - USING (time) -``` - -**Result:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -- **Retaining NULL Values**: When NULL values have special significance or when you wish to preserve the null values in the data, you can choose to omit FILL METHOD PREVIOUS to avoid filling in the gaps. -**Example:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) B - USING (time) -``` - -**Result:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| null| -|2025-05-13T00:00:03.000+08:00| null| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` -#### 3.6.2 Different Sampling Frequencies, Different Times - -**Table 1: Sampling Frequency: 1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**Table 3: Sampling Frequency: 2s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.005 | d1 | 35.1 | -| 00:00:02.106 | d1 | 37.2 | -| 00:00:04.187 | d1 | null | -| 00:00:06.156 | d1 | 35.1 | - -**Example: Querying the downsampled data of table1:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` -**Example: Querying the downsampled data of table3:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**Result:** - -```SQL -+-----------------------------+-------+ -| c_time|c_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 35.1| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 37.2| -|2025-05-13T00:00:04.000+08:00| 37.2| -|2025-05-13T00:00:05.000+08:00| 37.2| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**Example: Aligning multiple sequences by the higher sampling frequency:** - -```SQL -IoTDB> SELECT time, - a_value, - c_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) C - USING (time) -``` - -**Result:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|c_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -### 3.7 Missing Data Filling - -**Example**: Query the records within a specified time range where `device_id` is '100'. If there are missing data points, fill them using the previous non-null value. - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 - AND region='East' AND plant_id='1001' AND device_id='101' - FILL METHOD PREVIOUS; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.101s -``` - -### 3.8 Sorting & Pagination - -**Example**: Query records from the table, sorting by `humidity` in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows. - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - ORDER BY humidity desc NULLS LAST - OFFSET 2 - LIMIT 10; -``` - -**Result**: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 10 -It costs 0.093s -``` diff --git a/src/UserGuide/latest-Table/Basic-Concept/Query-Data_apache.md b/src/UserGuide/latest-Table/Basic-Concept/Query-Data_apache.md new file mode 100644 index 000000000..b3018d2f5 --- /dev/null +++ b/src/UserGuide/latest-Table/Basic-Concept/Query-Data_apache.md @@ -0,0 +1,589 @@ + + +# Query Data + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) + + +## 3. Common Query Examples + +### 3.1 Sample Dataset + +The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. + +### 3.2 Basic Data Query + +**Example 1: Filter by Time** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**Example 2: Filter by** **Value** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**Example 3: Filter by Attribute** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**Example 3:Multi device time aligned query** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 Aggregation Query + +**Example**: Calculate the average, maximum, and minimum temperature for each `device_id` within a specific time range. + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 Latest Point Query + +**Example**: Retrieve the latest record for each `device_id`, including the temperature value and the timestamp of the last record. + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 Downsampling Query + +**Example**: Group data by day and calculate the average temperature using `date_bin_gapfill` function. + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +**Result**: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` +### 3.6 Multi device downsampling alignment query + +#### 3.6.1 Sampling Frequency is the Same, but Time is Different + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 2: Sampling Frequency: 1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**Example: Querying the downsampled data of table2:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by integer time:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- **Retaining NULL Values**: When NULL values have special significance or when you wish to preserve the null values in the data, you can choose to omit FILL METHOD PREVIOUS to avoid filling in the gaps. +**Example:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 Different Sampling Frequencies, Different Times + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 3: Sampling Frequency: 2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**Example: Querying the downsampled data of table3:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by the higher sampling frequency:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 Missing Data Filling + +**Example**: Query the records within a specified time range where `device_id` is '100'. If there are missing data points, fill them using the previous non-null value. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='East' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 Sorting & Pagination + +**Example**: Query records from the table, sorting by `humidity` in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` diff --git a/src/UserGuide/latest-Table/Basic-Concept/Query-Data_timecho.md b/src/UserGuide/latest-Table/Basic-Concept/Query-Data_timecho.md new file mode 100644 index 000000000..659cbfd73 --- /dev/null +++ b/src/UserGuide/latest-Table/Basic-Concept/Query-Data_timecho.md @@ -0,0 +1,666 @@ + + +# Query Data + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **patternRecognition**: Row Pattern Recognition, which supports capturing a segment of continuous data by defining recognition logic for pattern variables and regular expressions, and performs analysis and calculation on each captured data segment. Details:[Row Pattern Recognition](../SQL-Manual/Row-Pattern-Recognition.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **WINDOW FUNCTION**: Window Function, a special function that performs calculations on each row based on a specific set of rows related to the current row (called a "window"). It combines grouping operations, sorting, and definable calculation ranges to implement complex cross-row calculations without collapsing the original data rows. Details: [Window Function](../SQL-Manual/Featured-Functions_timecho.md#_4-Window-Function) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/data-query-1.png) + + +## 3. Common Query Examples + +### 3.1 Sample Dataset + +The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. + +### 3.2 Basic Data Query + +**Example 1: Filter by Time** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**Example 2: Filter by** **Value** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**Example 3: Filter by Attribute** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**Example 3:Multi device time aligned query** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 Aggregation Query + +**Example**: Calculate the average, maximum, and minimum temperature for each `device_id` within a specific time range. + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 Latest Point Query + +**Example**: Retrieve the latest record for each `device_id`, including the temperature value and the timestamp of the last record. + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +**Result**: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 Downsampling Query + +**Example**: Group data by day and calculate the average temperature using `date_bin_gapfill` function. + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +**Result**: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` +### 3.6 Multi device downsampling alignment query + +#### 3.6.1 Sampling Frequency is the Same, but Time is Different + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 2: Sampling Frequency: 1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**Example: Querying the downsampled data of table2:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by integer time:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- **Retaining NULL Values**: When NULL values have special significance or when you wish to preserve the null values in the data, you can choose to omit FILL METHOD PREVIOUS to avoid filling in the gaps. +**Example:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 Different Sampling Frequencies, Different Times + +**Table 1: Sampling Frequency: 1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**Table 3: Sampling Frequency: 2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**Example: Querying the downsampled data of table1:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**Example: Querying the downsampled data of table3:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**Result:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**Example: Aligning multiple sequences by the higher sampling frequency:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**Result:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 Missing Data Filling + +**Example**: Query the records within a specified time range where `device_id` is '100'. If there are missing data points, fill them using the previous non-null value. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='East' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 Sorting & Pagination + +**Example**: Query records from the table, sorting by `humidity` in descending order and placing null values (NULL) at the end. Skip the first 2 rows and return the next 8 rows. + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +**Result**: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` + +### 3.9 Row Pattern Recognition + +**Example**: Segment data in table1 by time intervals of 24 hours or less, and query the total number of data entries in each segment, as well as the start and end times. + +```SQL +SELECT start_time, end_time, cnt +FROM table1 +MATCH_RECOGNIZE ( + ORDER BY time + MEASURES + RPR_FIRST(A.time) AS start_time, + RPR_LAST(time) AS end_time, + COUNT() AS cnt + PATTERN (A B*) + DEFINE B AS (cast(B.time as INT64) - cast(PREV(B.time) as INT64)) <= 86400000 +) AS m +``` + +**Result**: + +```SQL ++-----------------------------+-----------------------------+---+ +| start_time| end_time|cnt| ++-----------------------------+-----------------------------+---+ +|2024-11-26T13:37:00.000+08:00|2024-11-26T13:38:00.000+08:00| 2| +|2024-11-27T16:38:00.000+08:00|2024-11-30T14:30:00.000+08:00| 16| ++-----------------------------+-----------------------------+---+ +Total line number = 2 +``` + +### 3.10 Window Functions + +**Example**: Query the cumulative power consumption values of different devices. + +The original data is as follows: + +```SQL ++-----------------------------+------+-----+ +| time|device| flow| ++-----------------------------+------+-----+ +|1970-01-01T08:00:00.000+08:00| d0| 3| +|1970-01-01T08:00:00.001+08:00| d0| 5| +|1970-01-01T08:00:00.002+08:00| d0| 3| +|1970-01-01T08:00:00.003+08:00| d0| 1| +|1970-01-01T08:00:00.004+08:00| d1| 2| +|1970-01-01T08:00:00.005+08:00| d1| 4| ++-----------------------------+------+-----+ +``` + +**Query Statement**: + +```SQL +IoTDB> SELECT *, sum(flow) OVER(PARTITION BY device ORDER BY flow) as sum FROM device_flow; +``` + +After grouping, sorting, and calculation (steps are disassembled as shown in the figure below), + +![](/img/window-function-1.png) + +**Result**: + +```SQL ++-----------------------------+------+----+----+ +| time|device|flow| sum| ++-----------------------------+------+----+----+ +|1970-01-01T08:00:04.000+08:00| d1| 2| 2.0| +|1970-01-01T08:00:05.000+08:00| d1| 4| 6.0| +|1970-01-01T08:00:03.000+08:00| d0| 1| 1.0| +|1970-01-01T08:00:00.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:02.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:01.000+08:00| d0| 5|12.0| ++-----------------------------+------+----+----+ +``` diff --git a/src/UserGuide/latest-Table/SQL-Manual/overview.md b/src/UserGuide/latest-Table/SQL-Manual/overview.md index 0ef5cbc2e..28ad594b5 100644 --- a/src/UserGuide/latest-Table/SQL-Manual/overview.md +++ b/src/UserGuide/latest-Table/SQL-Manual/overview.md @@ -1,3 +1,6 @@ +--- +redirectTo: overview_apache.html +--- - -# Overview - -## 1. Syntax Overview - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -The IoTDB table model query syntax supports the following clauses: - -- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) -- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) -- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) -- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) -- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) -- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) -- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) -- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) -- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. Clause Execution Order - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) \ No newline at end of file diff --git a/src/UserGuide/latest-Table/SQL-Manual/overview_apache.md b/src/UserGuide/latest-Table/SQL-Manual/overview_apache.md new file mode 100644 index 000000000..0ef5cbc2e --- /dev/null +++ b/src/UserGuide/latest-Table/SQL-Manual/overview_apache.md @@ -0,0 +1,52 @@ + + +# Overview + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) \ No newline at end of file diff --git a/src/UserGuide/latest-Table/SQL-Manual/overview_timecho.md b/src/UserGuide/latest-Table/SQL-Manual/overview_timecho.md new file mode 100644 index 000000000..a7be3102b --- /dev/null +++ b/src/UserGuide/latest-Table/SQL-Manual/overview_timecho.md @@ -0,0 +1,55 @@ + + +# Overview + +## 1. Syntax Overview + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +The IoTDB table model query syntax supports the following clauses: + +- **SELECT Clause**: Specifies the columns to be included in the result. Details: [SELECT Clause](../SQL-Manual/Select-Clause.md) +- **FROM Clause**: Indicates the data source for the query, which can be a single table, multiple tables joined using the `JOIN` clause, or a subquery. Details: [FROM & JOIN Clause](../SQL-Manual/From-Join-Clause.md) +- **patternRecognition**: Row Pattern Recognition, which supports capturing a segment of continuous data by defining recognition logic for pattern variables and regular expressions, and performs analysis and calculation on each captured data segment. Details:[Row Pattern Recognition](../SQL-Manual/Row-Pattern-Recognition.md) +- **WHERE Clause**: Filters rows based on specific conditions. Logically executed immediately after the `FROM` clause. Details: [WHERE Clause](../SQL-Manual/Where-Clause.md) +- **GROUP BY Clause**: Used for aggregating data, specifying the columns for grouping. Details: [GROUP BY Clause](../SQL-Manual/GroupBy-Clause.md) +- **HAVING Clause**: Applied after the `GROUP BY` clause to filter grouped data, similar to `WHERE` but operates after grouping. Details:[HAVING Clause](../SQL-Manual/Having-Clause.md) +- **WINDOW FUNCTION**: Window Function, a special function that performs calculations on each row based on a specific set of rows related to the current row (called a "window"). It combines grouping operations, sorting, and definable calculation ranges to implement complex cross-row calculations without collapsing the original data rows. Details: [Window Function](../SQL-Manual/Featured-Functions_timecho.md#_4-Window-Function) +- **FILL Clause**: Handles missing values in query results by specifying fill methods (e.g., previous non-null value or linear interpolation) for better visualization and analysis. Details:[FILL Clause](../SQL-Manual/Fill-Clause.md) +- **ORDER BY Clause**: Sorts query results in ascending (`ASC`) or descending (`DESC`) order, with optional handling for null values (`NULLS FIRST` or `NULLS LAST`). Details: [ORDER BY Clause](../SQL-Manual/OrderBy-Clause.md) +- **OFFSET Clause**: Specifies the starting position for the query result, skipping the first `OFFSET` rows. Often used with the `LIMIT` clause. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) +- **LIMIT Clause**: Limits the number of rows in the query result. Typically used in conjunction with the `OFFSET` clause for pagination. Details: [LIMIT and OFFSET Clause](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. Clause Execution Order + +![](/img/data-query-1.png) \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data.md index 811f816d5..fd9f42512 100644 --- a/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data.md +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Query-Data_apache.html +--- - -# 数据查询 - -## 1. 语法概览 - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -IoTDB 查询语法提供以下子句: - -- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) -- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) -- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) -- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) -- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) -- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) -- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) -- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) -- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. 子句执行顺序 - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) - - - -## 3. 常见查询示例 - -### 3.1 示例数据 - -在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 - -### 3.2 原始数据查询 - -**示例1:根据时间过滤** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T08:00:00.000+08:00| 85.0| null| -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 11 -It costs 0.075s -``` - -**示例2:根据值过滤** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE temperature > 89.0; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 5 -It costs 0.156s -``` - -**示例3:根据属性过滤** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE model_id ='B'; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.106s -``` - -**示例4:多设备按时间对齐查询** - -```SQL -IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, - device_id, - AVG(temperature) AS avg_temp - FROM table1 - WHERE TIME >= 2024-11-26 13:00:00 - AND TIME <= 2024-11-27 17:00:00 - GROUP BY 1, device_id FILL METHOD PREVIOUS; -``` - -执行结果如下: - -```SQL -+-----------------------------+---------+--------+ -| a_time|device_id|avg_temp| -+-----------------------------+---------+--------+ -|2024-11-26T08:00:00.000+08:00| 100| 90.0| -|2024-11-27T08:00:00.000+08:00| 100| 90.0| -|2024-11-26T08:00:00.000+08:00| 101| 90.0| -|2024-11-27T08:00:00.000+08:00| 101| 85.0| -+-----------------------------+---------+--------+ -Total line number = 4 -It costs 0.048s -``` - -### 3.3 聚合查询 - -**示例:查询计算了在指定时间范围内,每个`device_id`的平均温度、最高温度和最低温度。** - -```SQL -IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 - GROUP BY device_id; -``` - -执行结果如下: - -```SQL -+---------+--------+--------+--------+ -|device_id|avg_temp|max_temp|min_temp| -+---------+--------+--------+--------+ -| 100| 87.6| 90.0| 85.0| -| 101| 85.0| 85.0| 85.0| -+---------+--------+--------+--------+ -Total line number = 2 -It costs 0.278s -``` - -### 3.4 最新点查询 - -**示例:查询表中每个 `device_id` 返回最后一条记录,包含该记录的温度值以及在该设备中基于时间和温度排序的最后一条记录。** - -```SQL -IoTDB> SELECT device_id,last(time),last_by(temperature,time) - FROM table1 - GROUP BY device_id; -``` - -执行结果如下: - -```SQL -+---------+-----------------------------+-----+ -|device_id| _col1|_col2| -+---------+-----------------------------+-----+ -| 100|2024-11-29T18:30:00.000+08:00| 90.0| -| 101|2024-11-30T14:30:00.000+08:00| 90.0| -+---------+-----------------------------+-----+ -Total line number = 2 -It costs 0.090s -``` - -### 3.5 降采样查询(date_bin 函数) - -**示例:查询将时间按天分组,并计算每天的平均温度。** - -```SQL -IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 - GROUP BY device_id,date_bin(1d ,time); -``` - -执行结果如下: - -```SQL -+---------+-----------------------------+--------+ -|device_id| day_time|avg_temp| -+---------+-----------------------------+--------+ -| 100|2024-11-29T08:00:00.000+08:00| 90.0| -| 100|2024-11-28T08:00:00.000+08:00| 86.0| -| 100|2024-11-26T08:00:00.000+08:00| 90.0| -| 101|2024-11-29T08:00:00.000+08:00| 85.0| -| 101|2024-11-27T08:00:00.000+08:00| 85.0| -+---------+-----------------------------+--------+ -Total line number = 5 -It costs 0.066s -``` - -### 3.6 多设备降采样对齐查询 - -#### 3.6.1 采样频率相同,时间不同 - -**table1:采样频率:1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**table2:采样频率:1s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.003 | d1 | 35.1 | -| 00:00:01.012 | d1 | 37.2 | -| 00:00:02.031 | d1 | null | -| 00:00:03.134 | d1 | 35.2 | -| 00:00:04.201 | d1 | 38.2 | -| 00:00:05.091 | d1 | 35.4 | -| 00:00:06.231 | d1 | 35.1 | - -**示例:查询`table1`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` - -**示例:查询`table2`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| b_time|b_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 37.2| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 35.2| -|2025-05-13T00:00:04.000+08:00| 38.2| -|2025-05-13T00:00:05.000+08:00| 35.4| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**示例:按整点将多个序列进行时间对齐:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) B - USING (time) -``` - -**结果:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -- 保留空值:当 `NULL` 值本身具有特殊含义,或希望保留数据的 null 值时,可以选择去掉 `FILL METHOD PREVIOUS` 不进行填充。 -**示例:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) B - USING (time) -``` - -**结果:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| null| -|2025-05-13T00:00:03.000+08:00| null| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` -#### 3.6.2 采样频率不同,时间不同 - -**table1:采样频率:1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**table3: 采样频率:2s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.005 | d1 | 35.1 | -| 00:00:02.106 | d1 | 37.2 | -| 00:00:04.187 | d1 | null | -| 00:00:06.156 | d1 | 35.1 | - -**示例:查询`table1`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` -**示例:查询`table3`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| c_time|c_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 35.1| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 37.2| -|2025-05-13T00:00:04.000+08:00| 37.2| -|2025-05-13T00:00:05.000+08:00| 37.2| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**示例:按照高采样频率进行对齐:** - -```SQL -IoTDB> SELECT time, - a_value, - c_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) C - USING (time) -``` - -**结果:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|c_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -### 3.7 数据填充 - -**示例:查询指定时间范围内,满足 `device_id` 为 '100' 的记录,若存在缺失的数据点,则用前一个非空值进行填充。** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 - AND region='北京' AND plant_id='1001' AND device_id='101' - FILL METHOD PREVIOUS; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.101s -``` - -### 3.8 排序&分页 - -**示例:查询表中湿度降序排列且空值(NULL)排最后的记录,跳过前 2 条,只返回接下来的 8 条记录。** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - ORDER BY humidity desc NULLS LAST - OFFSET 2 - LIMIT 10; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 10 -It costs 0.093s -``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data_apache.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data_apache.md new file mode 100644 index 000000000..811f816d5 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data_apache.md @@ -0,0 +1,591 @@ + + +# 数据查询 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) + + + +## 3. 常见查询示例 + +### 3.1 示例数据 + +在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 + +### 3.2 原始数据查询 + +**示例1:根据时间过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**示例2:根据值过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**示例3:根据属性过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**示例4:多设备按时间对齐查询** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 聚合查询 + +**示例:查询计算了在指定时间范围内,每个`device_id`的平均温度、最高温度和最低温度。** + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 最新点查询 + +**示例:查询表中每个 `device_id` 返回最后一条记录,包含该记录的温度值以及在该设备中基于时间和温度排序的最后一条记录。** + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 降采样查询(date_bin 函数) + +**示例:查询将时间按天分组,并计算每天的平均温度。** + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` + +### 3.6 多设备降采样对齐查询 + +#### 3.6.1 采样频率相同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table2:采样频率:1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**示例:查询`table2`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按整点将多个序列进行时间对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- 保留空值:当 `NULL` 值本身具有特殊含义,或希望保留数据的 null 值时,可以选择去掉 `FILL METHOD PREVIOUS` 不进行填充。 +**示例:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 采样频率不同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table3: 采样频率:2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**示例:查询`table3`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按照高采样频率进行对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 数据填充 + +**示例:查询指定时间范围内,满足 `device_id` 为 '100' 的记录,若存在缺失的数据点,则用前一个非空值进行填充。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='北京' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 排序&分页 + +**示例:查询表中湿度降序排列且空值(NULL)排最后的记录,跳过前 2 条,只返回接下来的 8 条记录。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data_timecho.md b/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data_timecho.md new file mode 100644 index 000000000..f9cfeb5ac --- /dev/null +++ b/src/zh/UserGuide/Master/Table/Basic-Concept/Query-Data_timecho.md @@ -0,0 +1,668 @@ + + +# 数据查询 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- patternRecognition:行模式识别,支持通过定义模式变量的识别逻辑以及正则表达式来捕获一段连续的数据,并对每一段捕获的数据进行分析计算。详细语法见:[行模式识别](../SQL-Manual/Row-Pattern-Recognition.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- WINDOW FUNCTION:窗口函数,是一种基于与当前行相关的特定行集合(称为“窗口”)​ 对每一行进行计算的特殊函数。它将分组操作、排序与可定义的计算范围结合,在不折叠原始数据行的前提下实现复杂的跨行计算。详细语法见:[窗口函数](../SQL-Manual/Featured-Functions_timecho.md#_2-窗口函数) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + +![](/img/data-query-1.png) + + + +## 3. 常见查询示例 + +### 3.1 示例数据 + +在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 + +### 3.2 原始数据查询 + +**示例1:根据时间过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**示例2:根据值过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**示例3:根据属性过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**示例4:多设备按时间对齐查询** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 聚合查询 + +**示例:查询计算了在指定时间范围内,每个`device_id`的平均温度、最高温度和最低温度。** + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 最新点查询 + +**示例:查询表中每个 `device_id` 返回最后一条记录,包含该记录的温度值以及在该设备中基于时间和温度排序的最后一条记录。** + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 降采样查询(date_bin 函数) + +**示例:查询将时间按天分组,并计算每天的平均温度。** + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` + +### 3.6 多设备降采样对齐查询 + +#### 3.6.1 采样频率相同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table2:采样频率:1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**示例:查询`table2`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按整点将多个序列进行时间对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- 保留空值:当 `NULL` 值本身具有特殊含义,或希望保留数据的 null 值时,可以选择去掉 `FILL METHOD PREVIOUS` 不进行填充。 +**示例:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 采样频率不同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table3: 采样频率:2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**示例:查询`table3`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按照高采样频率进行对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 数据填充 + +**示例:查询指定时间范围内,满足 `device_id` 为 '100' 的记录,若存在缺失的数据点,则用前一个非空值进行填充。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='北京' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 排序&分页 + +**示例:查询表中湿度降序排列且空值(NULL)排最后的记录,跳过前 2 条,只返回接下来的 8 条记录。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` + +### 3.9 行模式识别 + +**示例:将 table1 中的数据按照时间间隔小于等于 24 小时分段,查询每段中的数据总条数,以及开始、结束时间。** + +```SQL +SELECT start_time, end_time, cnt +FROM table1 +MATCH_RECOGNIZE ( + ORDER BY time + MEASURES + RPR_FIRST(A.time) AS start_time, + RPR_LAST(time) AS end_time, + COUNT() AS cnt + PATTERN (A B*) + DEFINE B AS (cast(B.time as INT64) - cast(PREV(B.time) as INT64)) <= 86400000 +) AS m +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------------------------+---+ +| start_time| end_time|cnt| ++-----------------------------+-----------------------------+---+ +|2024-11-26T13:37:00.000+08:00|2024-11-26T13:38:00.000+08:00| 2| +|2024-11-27T16:38:00.000+08:00|2024-11-30T14:30:00.000+08:00| 16| ++-----------------------------+-----------------------------+---+ +Total line number = 2 +``` + +### 3.10 窗口函数 + +**示例:查询不同设备的功耗累加值** + +原始数据如下: + +```SQL ++-----------------------------+------+-----+ +| time|device| flow| ++-----------------------------+------+-----+ +|1970-01-01T08:00:00.000+08:00| d0| 3| +|1970-01-01T08:00:00.001+08:00| d0| 5| +|1970-01-01T08:00:00.002+08:00| d0| 3| +|1970-01-01T08:00:00.003+08:00| d0| 1| +|1970-01-01T08:00:00.004+08:00| d1| 2| +|1970-01-01T08:00:00.005+08:00| d1| 4| ++-----------------------------+------+-----+ +``` + +查询语句如下: + +```SQL +IoTDB> SELECT *, sum(flow) OVER(PARTITION BY device ORDER BY flow) as sum​ ​FROM device_flow; +``` + +经过分组、排序、计算(步骤拆解如下图所示), + +![](/img/window-function-1.png) + +执行结果如下: + +```SQL ++-----------------------------+------+----+----+ +| time|device|flow| sum| ++-----------------------------+------+----+----+ +|1970-01-01T08:00:04.000+08:00| d1| 2| 2.0| +|1970-01-01T08:00:05.000+08:00| d1| 4| 6.0| +|1970-01-01T08:00:03.000+08:00| d0| 1| 1.0| +|1970-01-01T08:00:00.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:02.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:01.000+08:00| d0| 5|12.0| ++-----------------------------+------+----+----+ +``` diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/overview.md b/src/zh/UserGuide/Master/Table/SQL-Manual/overview.md index ea5a1a9cf..28ad594b5 100644 --- a/src/zh/UserGuide/Master/Table/SQL-Manual/overview.md +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/overview.md @@ -1,3 +1,6 @@ +--- +redirectTo: overview_apache.html +--- - -# 概览 - -## 1. 语法概览 - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -IoTDB 查询语法提供以下子句: - -- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) -- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) -- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) -- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) -- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) -- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) -- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) -- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) -- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. 子句执行顺序 - - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/overview_apache.md b/src/zh/UserGuide/Master/Table/SQL-Manual/overview_apache.md new file mode 100644 index 000000000..ea5a1a9cf --- /dev/null +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/overview_apache.md @@ -0,0 +1,53 @@ + + +# 概览 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/overview_timecho.md b/src/zh/UserGuide/Master/Table/SQL-Manual/overview_timecho.md new file mode 100644 index 000000000..ce12f25a1 --- /dev/null +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/overview_timecho.md @@ -0,0 +1,56 @@ + + +# 概览 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- patternRecognition:行模式识别,支持通过定义模式变量的识别逻辑以及正则表达式来捕获一段连续的数据,并对每一段捕获的数据进行分析计算。详细语法见:[行模式识别](../SQL-Manual/Row-Pattern-Recognition.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- WINDOW FUNCTION:窗口函数,是一种基于与当前行相关的特定行集合(称为“窗口”)​ 对每一行进行计算的特殊函数。它将分组操作、排序与可定义的计算范围结合,在不折叠原始数据行的前提下实现复杂的跨行计算。详细语法见:[窗口函数](../SQL-Manual/Featured-Functions_timecho.md#_2-窗口函数) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + + +![](/img/data-query-1.png) diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data.md index 811f816d5..fd9f42512 100644 --- a/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data.md +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data.md @@ -1,3 +1,6 @@ +--- +redirectTo: Query-Data_apache.html +--- - -# 数据查询 - -## 1. 语法概览 - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -IoTDB 查询语法提供以下子句: - -- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) -- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) -- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) -- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) -- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) -- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) -- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) -- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) -- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. 子句执行顺序 - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) - - - -## 3. 常见查询示例 - -### 3.1 示例数据 - -在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 - -### 3.2 原始数据查询 - -**示例1:根据时间过滤** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T08:00:00.000+08:00| 85.0| null| -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 11 -It costs 0.075s -``` - -**示例2:根据值过滤** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE temperature > 89.0; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 5 -It costs 0.156s -``` - -**示例3:根据属性过滤** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE model_id ='B'; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| null| -|2024-11-27T16:41:00.000+08:00| 85.0| null| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-27T16:43:00.000+08:00| null| null| -|2024-11-27T16:44:00.000+08:00| null| null| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.106s -``` - -**示例4:多设备按时间对齐查询** - -```SQL -IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, - device_id, - AVG(temperature) AS avg_temp - FROM table1 - WHERE TIME >= 2024-11-26 13:00:00 - AND TIME <= 2024-11-27 17:00:00 - GROUP BY 1, device_id FILL METHOD PREVIOUS; -``` - -执行结果如下: - -```SQL -+-----------------------------+---------+--------+ -| a_time|device_id|avg_temp| -+-----------------------------+---------+--------+ -|2024-11-26T08:00:00.000+08:00| 100| 90.0| -|2024-11-27T08:00:00.000+08:00| 100| 90.0| -|2024-11-26T08:00:00.000+08:00| 101| 90.0| -|2024-11-27T08:00:00.000+08:00| 101| 85.0| -+-----------------------------+---------+--------+ -Total line number = 4 -It costs 0.048s -``` - -### 3.3 聚合查询 - -**示例:查询计算了在指定时间范围内,每个`device_id`的平均温度、最高温度和最低温度。** - -```SQL -IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 - GROUP BY device_id; -``` - -执行结果如下: - -```SQL -+---------+--------+--------+--------+ -|device_id|avg_temp|max_temp|min_temp| -+---------+--------+--------+--------+ -| 100| 87.6| 90.0| 85.0| -| 101| 85.0| 85.0| 85.0| -+---------+--------+--------+--------+ -Total line number = 2 -It costs 0.278s -``` - -### 3.4 最新点查询 - -**示例:查询表中每个 `device_id` 返回最后一条记录,包含该记录的温度值以及在该设备中基于时间和温度排序的最后一条记录。** - -```SQL -IoTDB> SELECT device_id,last(time),last_by(temperature,time) - FROM table1 - GROUP BY device_id; -``` - -执行结果如下: - -```SQL -+---------+-----------------------------+-----+ -|device_id| _col1|_col2| -+---------+-----------------------------+-----+ -| 100|2024-11-29T18:30:00.000+08:00| 90.0| -| 101|2024-11-30T14:30:00.000+08:00| 90.0| -+---------+-----------------------------+-----+ -Total line number = 2 -It costs 0.090s -``` - -### 3.5 降采样查询(date_bin 函数) - -**示例:查询将时间按天分组,并计算每天的平均温度。** - -```SQL -IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp - FROM table1 - WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 - GROUP BY device_id,date_bin(1d ,time); -``` - -执行结果如下: - -```SQL -+---------+-----------------------------+--------+ -|device_id| day_time|avg_temp| -+---------+-----------------------------+--------+ -| 100|2024-11-29T08:00:00.000+08:00| 90.0| -| 100|2024-11-28T08:00:00.000+08:00| 86.0| -| 100|2024-11-26T08:00:00.000+08:00| 90.0| -| 101|2024-11-29T08:00:00.000+08:00| 85.0| -| 101|2024-11-27T08:00:00.000+08:00| 85.0| -+---------+-----------------------------+--------+ -Total line number = 5 -It costs 0.066s -``` - -### 3.6 多设备降采样对齐查询 - -#### 3.6.1 采样频率相同,时间不同 - -**table1:采样频率:1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**table2:采样频率:1s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.003 | d1 | 35.1 | -| 00:00:01.012 | d1 | 37.2 | -| 00:00:02.031 | d1 | null | -| 00:00:03.134 | d1 | 35.2 | -| 00:00:04.201 | d1 | 38.2 | -| 00:00:05.091 | d1 | 35.4 | -| 00:00:06.231 | d1 | 35.1 | - -**示例:查询`table1`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` - -**示例:查询`table2`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| b_time|b_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 37.2| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 35.2| -|2025-05-13T00:00:04.000+08:00| 38.2| -|2025-05-13T00:00:05.000+08:00| 35.4| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**示例:按整点将多个序列进行时间对齐:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) B - USING (time) -``` - -**结果:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -- 保留空值:当 `NULL` 值本身具有特殊含义,或希望保留数据的 null 值时,可以选择去掉 `FILL METHOD PREVIOUS` 不进行填充。 -**示例:** - -```SQL -IoTDB> SELECT time, - a_value, - b_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS b_value - FROM table2 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1) B - USING (time) -``` - -**结果:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|b_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:02.000+08:00| 85.0| null| -|2025-05-13T00:00:03.000+08:00| null| 35.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` -#### 3.6.2 采样频率不同,时间不同 - -**table1:采样频率:1s** - -| Time | device_id | temperature | -| ------------ | --------- | ----------- | -| 00:00:00.001 | d1 | 90.0 | -| 00:00:01.002 | d1 | 85.0 | -| 00:00:02.101 | d1 | 85.0 | -| 00:00:03.201 | d1 | null | -| 00:00:04.105 | d1 | 90.0 | -| 00:00:05.023 | d1 | 85.0 | -| 00:00:06.129 | d1 | 90.0 | - -**table3: 采样频率:2s** - -| Time | device_id | humidity | -| ------------ | --------- | -------- | -| 00:00:00.005 | d1 | 35.1 | -| 00:00:02.106 | d1 | 37.2 | -| 00:00:04.187 | d1 | null | -| 00:00:06.156 | d1 | 35.1 | - -**示例:查询`table1`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| a_time|a_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| -|2025-05-13T00:00:01.000+08:00| 85.0| -|2025-05-13T00:00:02.000+08:00| 85.0| -|2025-05-13T00:00:03.000+08:00| 85.0| -|2025-05-13T00:00:04.000+08:00| 90.0| -|2025-05-13T00:00:05.000+08:00| 85.0| -|2025-05-13T00:00:06.000+08:00| 90.0| -+-----------------------------+-------+ -``` -**示例:查询`table3`的降采样数据:** - -```SQL -IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS -``` - -**结果:** - -```SQL -+-----------------------------+-------+ -| c_time|c_value| -+-----------------------------+-------+ -|2025-05-13T00:00:00.000+08:00| 35.1| -|2025-05-13T00:00:01.000+08:00| 35.1| -|2025-05-13T00:00:02.000+08:00| 37.2| -|2025-05-13T00:00:03.000+08:00| 37.2| -|2025-05-13T00:00:04.000+08:00| 37.2| -|2025-05-13T00:00:05.000+08:00| 37.2| -|2025-05-13T00:00:06.000+08:00| 35.1| -+-----------------------------+-------+ -``` - -**示例:按照高采样频率进行对齐:** - -```SQL -IoTDB> SELECT time, - a_value, - c_value - FROM - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(temperature) AS a_value - FROM table1 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) A - JOIN - (SELECT date_bin_gapfill(1s, TIME) AS time, - first(humidity) AS c_value - FROM table3 - WHERE device_id = 'd1' - AND TIME >= 2025-05-13 00:00:00.000 - AND TIME <= 2025-05-13 00:00:07.000 - GROUP BY 1 FILL METHOD PREVIOUS) C - USING (time) -``` - -**结果:** - -```SQL -+-----------------------------+-------+-------+ -| time|a_value|c_value| -+-----------------------------+-------+-------+ -|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| -|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| -|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| -|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| -|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| -+-----------------------------+-------+-------+ -``` - -### 3.7 数据填充 - -**示例:查询指定时间范围内,满足 `device_id` 为 '100' 的记录,若存在缺失的数据点,则用前一个非空值进行填充。** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 - AND region='北京' AND plant_id='1001' AND device_id='101' - FILL METHOD PREVIOUS; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| -|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| -|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| -+-----------------------------+-----------+--------+ -Total line number = 7 -It costs 0.101s -``` - -### 3.8 排序&分页 - -**示例:查询表中湿度降序排列且空值(NULL)排最后的记录,跳过前 2 条,只返回接下来的 8 条记录。** - -```SQL -IoTDB> SELECT time, temperature, humidity - FROM table1 - ORDER BY humidity desc NULLS LAST - OFFSET 2 - LIMIT 10; -``` - -执行结果如下: - -```SQL -+-----------------------------+-----------+--------+ -| time|temperature|humidity| -+-----------------------------+-----------+--------+ -|2024-11-28T09:00:00.000+08:00| null| 40.9| -|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| -|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| -|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| -|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| -|2024-11-27T16:42:00.000+08:00| null| 35.2| -|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| -|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| -|2024-11-27T16:38:00.000+08:00| null| 35.1| -|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| -+-----------------------------+-----------+--------+ -Total line number = 10 -It costs 0.093s -``` \ No newline at end of file diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data_apache.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data_apache.md new file mode 100644 index 000000000..811f816d5 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data_apache.md @@ -0,0 +1,591 @@ + + +# 数据查询 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) + + + +## 3. 常见查询示例 + +### 3.1 示例数据 + +在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 + +### 3.2 原始数据查询 + +**示例1:根据时间过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**示例2:根据值过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**示例3:根据属性过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**示例4:多设备按时间对齐查询** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 聚合查询 + +**示例:查询计算了在指定时间范围内,每个`device_id`的平均温度、最高温度和最低温度。** + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 最新点查询 + +**示例:查询表中每个 `device_id` 返回最后一条记录,包含该记录的温度值以及在该设备中基于时间和温度排序的最后一条记录。** + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 降采样查询(date_bin 函数) + +**示例:查询将时间按天分组,并计算每天的平均温度。** + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` + +### 3.6 多设备降采样对齐查询 + +#### 3.6.1 采样频率相同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table2:采样频率:1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**示例:查询`table2`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按整点将多个序列进行时间对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- 保留空值:当 `NULL` 值本身具有特殊含义,或希望保留数据的 null 值时,可以选择去掉 `FILL METHOD PREVIOUS` 不进行填充。 +**示例:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 采样频率不同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table3: 采样频率:2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**示例:查询`table3`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按照高采样频率进行对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 数据填充 + +**示例:查询指定时间范围内,满足 `device_id` 为 '100' 的记录,若存在缺失的数据点,则用前一个非空值进行填充。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='北京' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 排序&分页 + +**示例:查询表中湿度降序排列且空值(NULL)排最后的记录,跳过前 2 条,只返回接下来的 8 条记录。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` \ No newline at end of file diff --git a/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data_timecho.md b/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data_timecho.md new file mode 100644 index 000000000..f9cfeb5ac --- /dev/null +++ b/src/zh/UserGuide/latest-Table/Basic-Concept/Query-Data_timecho.md @@ -0,0 +1,668 @@ + + +# 数据查询 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- patternRecognition:行模式识别,支持通过定义模式变量的识别逻辑以及正则表达式来捕获一段连续的数据,并对每一段捕获的数据进行分析计算。详细语法见:[行模式识别](../SQL-Manual/Row-Pattern-Recognition.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- WINDOW FUNCTION:窗口函数,是一种基于与当前行相关的特定行集合(称为“窗口”)​ 对每一行进行计算的特殊函数。它将分组操作、排序与可定义的计算范围结合,在不折叠原始数据行的前提下实现复杂的跨行计算。详细语法见:[窗口函数](../SQL-Manual/Featured-Functions_timecho.md#_2-窗口函数) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + +![](/img/data-query-1.png) + + + +## 3. 常见查询示例 + +### 3.1 示例数据 + +在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 + +### 3.2 原始数据查询 + +**示例1:根据时间过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-27 00:00:00 and time <= 2024-11-29 00:00:00; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T08:00:00.000+08:00| 85.0| null| +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-28T11:00:00.000+08:00| 88.0| 45.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 11 +It costs 0.075s +``` + +**示例2:根据值过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE temperature > 89.0; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 5 +It costs 0.156s +``` + +**示例3:根据属性过滤** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE model_id ='B'; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| null| +|2024-11-27T16:41:00.000+08:00| 85.0| null| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-27T16:43:00.000+08:00| null| null| +|2024-11-27T16:44:00.000+08:00| null| null| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.106s +``` + +**示例4:多设备按时间对齐查询** + +```SQL +IoTDB> SELECT date_bin_gapfill(1d, TIME) AS a_time, + device_id, + AVG(temperature) AS avg_temp + FROM table1 + WHERE TIME >= 2024-11-26 13:00:00 + AND TIME <= 2024-11-27 17:00:00 + GROUP BY 1, device_id FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+---------+--------+ +| a_time|device_id|avg_temp| ++-----------------------------+---------+--------+ +|2024-11-26T08:00:00.000+08:00| 100| 90.0| +|2024-11-27T08:00:00.000+08:00| 100| 90.0| +|2024-11-26T08:00:00.000+08:00| 101| 90.0| +|2024-11-27T08:00:00.000+08:00| 101| 85.0| ++-----------------------------+---------+--------+ +Total line number = 4 +It costs 0.048s +``` + +### 3.3 聚合查询 + +**示例:查询计算了在指定时间范围内,每个`device_id`的平均温度、最高温度和最低温度。** + +```SQL +IoTDB> SELECT device_id, AVG(temperature) as avg_temp, MAX(temperature) as max_temp, MIN(temperature) as min_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-29 00:00:00 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+--------+--------+--------+ +|device_id|avg_temp|max_temp|min_temp| ++---------+--------+--------+--------+ +| 100| 87.6| 90.0| 85.0| +| 101| 85.0| 85.0| 85.0| ++---------+--------+--------+--------+ +Total line number = 2 +It costs 0.278s +``` + +### 3.4 最新点查询 + +**示例:查询表中每个 `device_id` 返回最后一条记录,包含该记录的温度值以及在该设备中基于时间和温度排序的最后一条记录。** + +```SQL +IoTDB> SELECT device_id,last(time),last_by(temperature,time) + FROM table1 + GROUP BY device_id; +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+-----+ +|device_id| _col1|_col2| ++---------+-----------------------------+-----+ +| 100|2024-11-29T18:30:00.000+08:00| 90.0| +| 101|2024-11-30T14:30:00.000+08:00| 90.0| ++---------+-----------------------------+-----+ +Total line number = 2 +It costs 0.090s +``` + +### 3.5 降采样查询(date_bin 函数) + +**示例:查询将时间按天分组,并计算每天的平均温度。** + +```SQL +IoTDB> SELECT device_id,date_bin(1d ,time) as day_time, AVG(temperature) as avg_temp + FROM table1 + WHERE time >= 2024-11-26 00:00:00 AND time <= 2024-11-30 00:00:00 + GROUP BY device_id,date_bin(1d ,time); +``` + +执行结果如下: + +```SQL ++---------+-----------------------------+--------+ +|device_id| day_time|avg_temp| ++---------+-----------------------------+--------+ +| 100|2024-11-29T08:00:00.000+08:00| 90.0| +| 100|2024-11-28T08:00:00.000+08:00| 86.0| +| 100|2024-11-26T08:00:00.000+08:00| 90.0| +| 101|2024-11-29T08:00:00.000+08:00| 85.0| +| 101|2024-11-27T08:00:00.000+08:00| 85.0| ++---------+-----------------------------+--------+ +Total line number = 5 +It costs 0.066s +``` + +### 3.6 多设备降采样对齐查询 + +#### 3.6.1 采样频率相同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table2:采样频率:1s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.003 | d1 | 35.1 | +| 00:00:01.012 | d1 | 37.2 | +| 00:00:02.031 | d1 | null | +| 00:00:03.134 | d1 | 35.2 | +| 00:00:04.201 | d1 | 38.2 | +| 00:00:05.091 | d1 | 35.4 | +| 00:00:06.231 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` + +**示例:查询`table2`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS b_time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| b_time|b_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 37.2| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 35.2| +|2025-05-13T00:00:04.000+08:00| 38.2| +|2025-05-13T00:00:05.000+08:00| 35.4| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按整点将多个序列进行时间对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +- 保留空值:当 `NULL` 值本身具有特殊含义,或希望保留数据的 null 值时,可以选择去掉 `FILL METHOD PREVIOUS` 不进行填充。 +**示例:** + +```SQL +IoTDB> SELECT time, + a_value, + b_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS b_value + FROM table2 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1) B + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|b_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:02.000+08:00| 85.0| null| +|2025-05-13T00:00:03.000+08:00| null| 35.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 38.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 35.4| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` +#### 3.6.2 采样频率不同,时间不同 + +**table1:采样频率:1s** + +| Time | device_id | temperature | +| ------------ | --------- | ----------- | +| 00:00:00.001 | d1 | 90.0 | +| 00:00:01.002 | d1 | 85.0 | +| 00:00:02.101 | d1 | 85.0 | +| 00:00:03.201 | d1 | null | +| 00:00:04.105 | d1 | 90.0 | +| 00:00:05.023 | d1 | 85.0 | +| 00:00:06.129 | d1 | 90.0 | + +**table3: 采样频率:2s** + +| Time | device_id | humidity | +| ------------ | --------- | -------- | +| 00:00:00.005 | d1 | 35.1 | +| 00:00:02.106 | d1 | 37.2 | +| 00:00:04.187 | d1 | null | +| 00:00:06.156 | d1 | 35.1 | + +**示例:查询`table1`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS a_time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| a_time|a_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| +|2025-05-13T00:00:01.000+08:00| 85.0| +|2025-05-13T00:00:02.000+08:00| 85.0| +|2025-05-13T00:00:03.000+08:00| 85.0| +|2025-05-13T00:00:04.000+08:00| 90.0| +|2025-05-13T00:00:05.000+08:00| 85.0| +|2025-05-13T00:00:06.000+08:00| 90.0| ++-----------------------------+-------+ +``` +**示例:查询`table3`的降采样数据:** + +```SQL +IoTDB> SELECT date_bin_gapfill(1s, TIME) AS c_time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS +``` + +**结果:** + +```SQL ++-----------------------------+-------+ +| c_time|c_value| ++-----------------------------+-------+ +|2025-05-13T00:00:00.000+08:00| 35.1| +|2025-05-13T00:00:01.000+08:00| 35.1| +|2025-05-13T00:00:02.000+08:00| 37.2| +|2025-05-13T00:00:03.000+08:00| 37.2| +|2025-05-13T00:00:04.000+08:00| 37.2| +|2025-05-13T00:00:05.000+08:00| 37.2| +|2025-05-13T00:00:06.000+08:00| 35.1| ++-----------------------------+-------+ +``` + +**示例:按照高采样频率进行对齐:** + +```SQL +IoTDB> SELECT time, + a_value, + c_value + FROM + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(temperature) AS a_value + FROM table1 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) A + JOIN + (SELECT date_bin_gapfill(1s, TIME) AS time, + first(humidity) AS c_value + FROM table3 + WHERE device_id = 'd1' + AND TIME >= 2025-05-13 00:00:00.000 + AND TIME <= 2025-05-13 00:00:07.000 + GROUP BY 1 FILL METHOD PREVIOUS) C + USING (time) +``` + +**结果:** + +```SQL ++-----------------------------+-------+-------+ +| time|a_value|c_value| ++-----------------------------+-------+-------+ +|2025-05-13T00:00:00.000+08:00| 90.0| 35.1| +|2025-05-13T00:00:01.000+08:00| 85.0| 35.1| +|2025-05-13T00:00:02.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:03.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:04.000+08:00| 90.0| 37.2| +|2025-05-13T00:00:05.000+08:00| 85.0| 37.2| +|2025-05-13T00:00:06.000+08:00| 90.0| 35.1| ++-----------------------------+-------+-------+ +``` + +### 3.7 数据填充 + +**示例:查询指定时间范围内,满足 `device_id` 为 '100' 的记录,若存在缺失的数据点,则用前一个非空值进行填充。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + WHERE time >= 2024-11-26 00:00:00 and time <= 2024-11-30 11:00:00 + AND region='北京' AND plant_id='1001' AND device_id='101' + FILL METHOD PREVIOUS; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:40:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:41:00.000+08:00| 85.0| 35.3| +|2024-11-27T16:42:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:43:00.000+08:00| 85.0| 35.2| +|2024-11-27T16:44:00.000+08:00| 85.0| 35.2| ++-----------------------------+-----------+--------+ +Total line number = 7 +It costs 0.101s +``` + +### 3.8 排序&分页 + +**示例:查询表中湿度降序排列且空值(NULL)排最后的记录,跳过前 2 条,只返回接下来的 8 条记录。** + +```SQL +IoTDB> SELECT time, temperature, humidity + FROM table1 + ORDER BY humidity desc NULLS LAST + OFFSET 2 + LIMIT 10; +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------+--------+ +| time|temperature|humidity| ++-----------------------------+-----------+--------+ +|2024-11-28T09:00:00.000+08:00| null| 40.9| +|2024-11-29T18:30:00.000+08:00| 90.0| 35.4| +|2024-11-27T16:39:00.000+08:00| 85.0| 35.3| +|2024-11-28T10:00:00.000+08:00| 85.0| 35.2| +|2024-11-30T09:30:00.000+08:00| 90.0| 35.2| +|2024-11-27T16:42:00.000+08:00| null| 35.2| +|2024-11-26T13:38:00.000+08:00| 90.0| 35.1| +|2024-11-26T13:37:00.000+08:00| 90.0| 35.1| +|2024-11-27T16:38:00.000+08:00| null| 35.1| +|2024-11-30T14:30:00.000+08:00| 90.0| 34.8| ++-----------------------------+-----------+--------+ +Total line number = 10 +It costs 0.093s +``` + +### 3.9 行模式识别 + +**示例:将 table1 中的数据按照时间间隔小于等于 24 小时分段,查询每段中的数据总条数,以及开始、结束时间。** + +```SQL +SELECT start_time, end_time, cnt +FROM table1 +MATCH_RECOGNIZE ( + ORDER BY time + MEASURES + RPR_FIRST(A.time) AS start_time, + RPR_LAST(time) AS end_time, + COUNT() AS cnt + PATTERN (A B*) + DEFINE B AS (cast(B.time as INT64) - cast(PREV(B.time) as INT64)) <= 86400000 +) AS m +``` + +执行结果如下: + +```SQL ++-----------------------------+-----------------------------+---+ +| start_time| end_time|cnt| ++-----------------------------+-----------------------------+---+ +|2024-11-26T13:37:00.000+08:00|2024-11-26T13:38:00.000+08:00| 2| +|2024-11-27T16:38:00.000+08:00|2024-11-30T14:30:00.000+08:00| 16| ++-----------------------------+-----------------------------+---+ +Total line number = 2 +``` + +### 3.10 窗口函数 + +**示例:查询不同设备的功耗累加值** + +原始数据如下: + +```SQL ++-----------------------------+------+-----+ +| time|device| flow| ++-----------------------------+------+-----+ +|1970-01-01T08:00:00.000+08:00| d0| 3| +|1970-01-01T08:00:00.001+08:00| d0| 5| +|1970-01-01T08:00:00.002+08:00| d0| 3| +|1970-01-01T08:00:00.003+08:00| d0| 1| +|1970-01-01T08:00:00.004+08:00| d1| 2| +|1970-01-01T08:00:00.005+08:00| d1| 4| ++-----------------------------+------+-----+ +``` + +查询语句如下: + +```SQL +IoTDB> SELECT *, sum(flow) OVER(PARTITION BY device ORDER BY flow) as sum​ ​FROM device_flow; +``` + +经过分组、排序、计算(步骤拆解如下图所示), + +![](/img/window-function-1.png) + +执行结果如下: + +```SQL ++-----------------------------+------+----+----+ +| time|device|flow| sum| ++-----------------------------+------+----+----+ +|1970-01-01T08:00:04.000+08:00| d1| 2| 2.0| +|1970-01-01T08:00:05.000+08:00| d1| 4| 6.0| +|1970-01-01T08:00:03.000+08:00| d0| 1| 1.0| +|1970-01-01T08:00:00.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:02.000+08:00| d0| 3| 7.0| +|1970-01-01T08:00:01.000+08:00| d0| 5|12.0| ++-----------------------------+------+----+----+ +``` diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/overview.md b/src/zh/UserGuide/latest-Table/SQL-Manual/overview.md index ea5a1a9cf..28ad594b5 100644 --- a/src/zh/UserGuide/latest-Table/SQL-Manual/overview.md +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/overview.md @@ -1,3 +1,6 @@ +--- +redirectTo: overview_apache.html +--- - -# 概览 - -## 1. 语法概览 - -```SQL -SELECT ⟨select_list⟩ - FROM ⟨tables⟩ - [WHERE ⟨condition⟩] - [GROUP BY ⟨groups⟩] - [HAVING ⟨group_filter⟩] - [FILL ⟨fill_methods⟩] - [ORDER BY ⟨order_expression⟩] - [OFFSET ⟨n⟩] - [LIMIT ⟨n⟩]; -``` - -IoTDB 查询语法提供以下子句: - -- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) -- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) -- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) -- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) -- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) -- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) -- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) -- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) -- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) - -## 2. 子句执行顺序 - - -![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/overview_apache.md b/src/zh/UserGuide/latest-Table/SQL-Manual/overview_apache.md new file mode 100644 index 000000000..ea5a1a9cf --- /dev/null +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/overview_apache.md @@ -0,0 +1,53 @@ + + +# 概览 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + + +![](/img/%E5%AD%90%E5%8F%A5%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F01.png) diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/overview_timecho.md b/src/zh/UserGuide/latest-Table/SQL-Manual/overview_timecho.md new file mode 100644 index 000000000..ce12f25a1 --- /dev/null +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/overview_timecho.md @@ -0,0 +1,56 @@ + + +# 概览 + +## 1. 语法概览 + +```SQL +SELECT ⟨select_list⟩ + FROM ⟨tables⟩ | patternRecognition + [WHERE ⟨condition⟩] + [GROUP BY ⟨groups⟩] + [HAVING ⟨group_filter⟩] + [WINDOW windowDefinition (',' windowDefinition)*)] + [FILL ⟨fill_methods⟩] + [ORDER BY ⟨order_expression⟩] + [OFFSET ⟨n⟩] + [LIMIT ⟨n⟩]; +``` + +IoTDB 查询语法提供以下子句: + +- SELECT 子句:查询结果应包含的列。详细语法见:[SELECT子句](../SQL-Manual/Select-Clause.md) +- FROM 子句:指出查询的数据源,可以是单个表、多个通过 `JOIN` 子句连接的表,或者是一个子查询。详细语法见:[FROM & JOIN 子句](../SQL-Manual/From-Join-Clause.md) +- patternRecognition:行模式识别,支持通过定义模式变量的识别逻辑以及正则表达式来捕获一段连续的数据,并对每一段捕获的数据进行分析计算。详细语法见:[行模式识别](../SQL-Manual/Row-Pattern-Recognition.md) +- WHERE 子句:用于过滤数据,只选择满足特定条件的数据行。这个子句在逻辑上紧跟在 FROM 子句之后执行。详细语法见:[WHERE 子句](../SQL-Manual/Where-Clause.md) +- GROUP BY 子句:当需要对数据进行聚合时使用,指定了用于分组的列。详细语法见:[GROUP BY 子句](../SQL-Manual/GroupBy-Clause.md) +- HAVING 子句:在 GROUP BY 子句之后使用,用于对已经分组的数据进行过滤。与 WHERE 子句类似,但 HAVING 子句在分组后执行。详细语法见:[HAVING 子句](../SQL-Manual/Having-Clause.md) +- WINDOW FUNCTION:窗口函数,是一种基于与当前行相关的特定行集合(称为“窗口”)​ 对每一行进行计算的特殊函数。它将分组操作、排序与可定义的计算范围结合,在不折叠原始数据行的前提下实现复杂的跨行计算。详细语法见:[窗口函数](../SQL-Manual/Featured-Functions_timecho.md#_2-窗口函数) +- FILL 子句:用于处理查询结果中的空值,用户可以使用 FILL 子句来指定数据缺失时的填充模式(如前一个非空值或线性插值)来填充 null 值,以便于数据可视化和分析。 详细语法见:[FILL 子句](../SQL-Manual/Fill-Clause.md) +- ORDER BY 子句:对查询结果进行排序,可以指定升序(ASC)或降序(DESC),以及 NULL 值的处理方式(NULLS FIRST 或 NULLS LAST)。详细语法见:[ORDER BY 子句](../SQL-Manual/OrderBy-Clause.md) +- OFFSET 子句:用于指定查询结果的起始位置,即跳过前 OFFSET 行。与 LIMIT 子句配合使用。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) +- LIMIT 子句:限制查询结果的行数,通常与 OFFSET 子句一起使用以实现分页功能。详细语法见:[LIMIT 和 OFFSET 子句](../SQL-Manual/Limit-Offset-Clause.md) + +## 2. 子句执行顺序 + + +![](/img/data-query-1.png)