From 05d2dd267c0870e0cab4a677474a57741999167e Mon Sep 17 00:00:00 2001 From: Leto_b Date: Thu, 23 Apr 2026 10:51:55 +0800 Subject: [PATCH] add set operations in table from 2091 --- src/.vuepress/sidebar/V2.0.x/en-Table.ts | 1 + src/.vuepress/sidebar/V2.0.x/zh-Table.ts | 1 + .../sidebar_timecho/V2.0.x/en-Table.ts | 1 + .../sidebar_timecho/V2.0.x/zh-Table.ts | 1 + .../Table/SQL-Manual/Set-Operations_apache.md | 295 ++++++++++++++++ .../SQL-Manual/Set-Operations_timecho.md | 295 ++++++++++++++++ .../SQL-Manual/Set-Operations_apache.md | 295 ++++++++++++++++ .../SQL-Manual/Set-Operations_timecho.md | 295 ++++++++++++++++ .../Table/SQL-Manual/Set-Operations_apache.md | 322 ++++++++++++++++++ .../SQL-Manual/Set-Operations_timecho.md | 322 ++++++++++++++++++ .../SQL-Manual/Set-Operations_apache.md | 322 ++++++++++++++++++ .../SQL-Manual/Set-Operations_timecho.md | 322 ++++++++++++++++++ 12 files changed, 2472 insertions(+) create mode 100644 src/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md create mode 100644 src/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md create mode 100644 src/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md create mode 100644 src/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md create mode 100644 src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md create mode 100644 src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md create mode 100644 src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md create mode 100644 src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md diff --git a/src/.vuepress/sidebar/V2.0.x/en-Table.ts b/src/.vuepress/sidebar/V2.0.x/en-Table.ts index e148e6ef6..aacd2678f 100644 --- a/src/.vuepress/sidebar/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/en-Table.ts @@ -233,6 +233,7 @@ export const enSidebar = { { text: 'ORDER BY Clause', link: 'OrderBy-Clause' }, { text: 'LIMIT&OFFSET Clause', link: 'Limit-Offset-Clause' }, { text: 'Nested Queries', link: 'Nested-Queries' }, + { text: 'Set Operations', link: 'Set-Operations_apache' }, ], }, { text: 'Maintenance Statements', link: 'SQL-Maintenance-Statements_apache' }, diff --git a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts index b66faad96..ef510ba61 100644 --- a/src/.vuepress/sidebar/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar/V2.0.x/zh-Table.ts @@ -230,6 +230,7 @@ export const zhSidebar = { { text: 'ORDER BY子句', link: 'OrderBy-Clause' }, { text: 'LIMIT&OFFSET子句', link: 'Limit-Offset-Clause' }, { text: '嵌套查询', link: 'Nested-Queries' }, + { text: '集合操作', link: 'Set-Operations_apache' }, ], }, { text: '运维语句', link: 'SQL-Maintenance-Statements_apache' }, 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 49272a093..4f9a391ca 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/en-Table.ts @@ -265,6 +265,7 @@ export const enSidebar = { { text: 'LIMIT&OFFSET Clause', link: 'Limit-Offset-Clause' }, { text: 'Nested Queries', link: 'Nested-Queries' }, { text: 'Pattern Query', link: 'Row-Pattern-Recognition_timecho' }, + { text: 'Set Operations', link: 'Set-Operations_timecho' }, ], }, { text: 'Maintenance Statements', link: 'SQL-Maintenance-Statements_timecho' }, 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 4400dcae3..c665d1a1d 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Table.ts @@ -256,6 +256,7 @@ export const zhSidebar = { { text: 'LIMIT&OFFSET子句', link: 'Limit-Offset-Clause' }, { text: '嵌套查询', link: 'Nested-Queries' }, { text: '模式查询', link: 'Row-Pattern-Recognition_timecho' }, + { text: '集合操作', link: 'Set-Operations_timecho' }, ], }, { text: '运维语句', link: 'SQL-Maintenance-Statements_timecho' }, diff --git a/src/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md b/src/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md new file mode 100644 index 000000000..5b617d31b --- /dev/null +++ b/src/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md @@ -0,0 +1,295 @@ + +# Set Operations + +IoTDB natively supports standard SQL set operations, including three core operators: **UNION**, **INTERSECT**, and **EXCEPT**. These operations enable seamless merging, comparison, and filtering of query results from multiple time-series data sources, greatly improving the flexibility and efficiency of time-series data analysis. + +> Note: This feature is available since version 2.0.9-beta. + +## 1. UNION +### 1.1 Overview +The UNION operator combines all rows from two result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 1.2 Syntax +```sql +query UNION (ALL | DISTINCT) query +``` + +**Description** +1. **Duplicate Handling** + - Default (`UNION` or `UNION DISTINCT`): Automatically removes duplicate rows. + - `UNION ALL`: Preserves all rows (including duplicates) with higher performance. + +2. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types: + - Numeric compatibility: `INT32`, `INT64`, `FLOAT`, and `DOUBLE` are fully compatible with each other. + - String compatibility: `TEXT` and `STRING` are fully compatible. + - Special rule: `INT64` is compatible with `TIMESTAMP`. + +3. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 1.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct non-null device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION DISTINCT +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. Get all non-null device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION ALL +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> **Notes** +> - Set operations **do not guarantee result order**; actual output may differ from examples. + + +## 2. INTERSECT +### 2.1 Overview +The INTERSECT operator returns rows that exist in both result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 2.2 Syntax +```sql +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`INTERSECT` or `INTERSECT DISTINCT`): Automatically removes duplicate rows. + - `INTERSECT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `INTERSECT` has higher precedence than `UNION` and `EXCEPT` + (e.g., `A UNION B INTERSECT C` is equivalent to `A UNION (B INTERSECT C)`). + - Evaluation is left-to-right + (e.g., `A INTERSECT B INTERSECT C` is equivalent to `(A INTERSECT B) INTERSECT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 2.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct common device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 +INTERSECT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +INTERSECT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| null| +| 100| 90.0| +| 100| 85.0| ++---------+-----------+ +Total line number = 5 +It costs 0.087s +``` + +2. Get all common device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +INTERSECT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| ++---------+-----------+ +Total line number = 6 +It costs 0.139s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`EXCEPT`, use parentheses to explicitly specify precedence + > (e.g., `A INTERSECT (B UNION C)`). + + +## 3. EXCEPT +### 3.1 Overview +The EXCEPT operator returns rows that exist in the first result set but **not** in the second (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 3.2 Syntax +```sql +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`EXCEPT` or `EXCEPT DISTINCT`): Automatically removes duplicate rows. + - `EXCEPT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `EXCEPT` has the same precedence as `UNION`, and lower precedence than `INTERSECT` + (e.g., `A INTERSECT B EXCEPT C` is equivalent to `(A INTERSECT B) EXCEPT C`). + - Evaluation is left-to-right + (e.g., `A EXCEPT B EXCEPT C` is equivalent to `(A EXCEPT B) EXCEPT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 3.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct records from `table1` that do not exist in `table2` +```sql +SELECT device_id, temperature FROM table1 +EXCEPT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +EXCEPT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| null| +| 100| 88.0| ++---------+-----------+ +Total line number = 2 +It costs 0.173s +``` + +2. Get all records from `table1` that do not exist in `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +EXCEPT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| +| 101| null| +| 101| null| +| 101| null| +| 101| null| ++---------+-----------+ +Total line number = 12 +It costs 0.155s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`INTERSECT`, use parentheses to explicitly specify precedence + > (e.g., `A EXCEPT (B INTERSECT C)`). \ No newline at end of file diff --git a/src/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md b/src/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md new file mode 100644 index 000000000..3628b15ec --- /dev/null +++ b/src/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md @@ -0,0 +1,295 @@ + +# Set Operations + +IoTDB natively supports standard SQL set operations, including three core operators: **UNION**, **INTERSECT**, and **EXCEPT**. These operations enable seamless merging, comparison, and filtering of query results from multiple time-series data sources, greatly improving the flexibility and efficiency of time-series data analysis. + +> Note: This feature is available since version 2.0.9.1. + +## 1. UNION +### 1.1 Overview +The UNION operator combines all rows from two result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 1.2 Syntax +```sql +query UNION (ALL | DISTINCT) query +``` + +**Description** +1. **Duplicate Handling** + - Default (`UNION` or `UNION DISTINCT`): Automatically removes duplicate rows. + - `UNION ALL`: Preserves all rows (including duplicates) with higher performance. + +2. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types: + - Numeric compatibility: `INT32`, `INT64`, `FLOAT`, and `DOUBLE` are fully compatible with each other. + - String compatibility: `TEXT` and `STRING` are fully compatible. + - Special rule: `INT64` is compatible with `TIMESTAMP`. + +3. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 1.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct non-null device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION DISTINCT +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. Get all non-null device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION ALL +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> **Notes** +> - Set operations **do not guarantee result order**; actual output may differ from examples. + + +## 2. INTERSECT +### 2.1 Overview +The INTERSECT operator returns rows that exist in both result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 2.2 Syntax +```sql +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`INTERSECT` or `INTERSECT DISTINCT`): Automatically removes duplicate rows. + - `INTERSECT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `INTERSECT` has higher precedence than `UNION` and `EXCEPT` + (e.g., `A UNION B INTERSECT C` is equivalent to `A UNION (B INTERSECT C)`). + - Evaluation is left-to-right + (e.g., `A INTERSECT B INTERSECT C` is equivalent to `(A INTERSECT B) INTERSECT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 2.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct common device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 +INTERSECT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +INTERSECT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| null| +| 100| 90.0| +| 100| 85.0| ++---------+-----------+ +Total line number = 5 +It costs 0.087s +``` + +2. Get all common device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +INTERSECT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| ++---------+-----------+ +Total line number = 6 +It costs 0.139s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`EXCEPT`, use parentheses to explicitly specify precedence + > (e.g., `A INTERSECT (B UNION C)`). + + +## 3. EXCEPT +### 3.1 Overview +The EXCEPT operator returns rows that exist in the first result set but **not** in the second (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 3.2 Syntax +```sql +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`EXCEPT` or `EXCEPT DISTINCT`): Automatically removes duplicate rows. + - `EXCEPT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `EXCEPT` has the same precedence as `UNION`, and lower precedence than `INTERSECT` + (e.g., `A INTERSECT B EXCEPT C` is equivalent to `(A INTERSECT B) EXCEPT C`). + - Evaluation is left-to-right + (e.g., `A EXCEPT B EXCEPT C` is equivalent to `(A EXCEPT B) EXCEPT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 3.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct records from `table1` that do not exist in `table2` +```sql +SELECT device_id, temperature FROM table1 +EXCEPT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +EXCEPT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| null| +| 100| 88.0| ++---------+-----------+ +Total line number = 2 +It costs 0.173s +``` + +2. Get all records from `table1` that do not exist in `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +EXCEPT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| +| 101| null| +| 101| null| +| 101| null| +| 101| null| ++---------+-----------+ +Total line number = 12 +It costs 0.155s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`INTERSECT`, use parentheses to explicitly specify precedence + > (e.g., `A EXCEPT (B INTERSECT C)`). \ No newline at end of file diff --git a/src/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md b/src/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md new file mode 100644 index 000000000..5b617d31b --- /dev/null +++ b/src/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md @@ -0,0 +1,295 @@ + +# Set Operations + +IoTDB natively supports standard SQL set operations, including three core operators: **UNION**, **INTERSECT**, and **EXCEPT**. These operations enable seamless merging, comparison, and filtering of query results from multiple time-series data sources, greatly improving the flexibility and efficiency of time-series data analysis. + +> Note: This feature is available since version 2.0.9-beta. + +## 1. UNION +### 1.1 Overview +The UNION operator combines all rows from two result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 1.2 Syntax +```sql +query UNION (ALL | DISTINCT) query +``` + +**Description** +1. **Duplicate Handling** + - Default (`UNION` or `UNION DISTINCT`): Automatically removes duplicate rows. + - `UNION ALL`: Preserves all rows (including duplicates) with higher performance. + +2. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types: + - Numeric compatibility: `INT32`, `INT64`, `FLOAT`, and `DOUBLE` are fully compatible with each other. + - String compatibility: `TEXT` and `STRING` are fully compatible. + - Special rule: `INT64` is compatible with `TIMESTAMP`. + +3. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 1.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct non-null device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION DISTINCT +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. Get all non-null device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION ALL +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> **Notes** +> - Set operations **do not guarantee result order**; actual output may differ from examples. + + +## 2. INTERSECT +### 2.1 Overview +The INTERSECT operator returns rows that exist in both result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 2.2 Syntax +```sql +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`INTERSECT` or `INTERSECT DISTINCT`): Automatically removes duplicate rows. + - `INTERSECT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `INTERSECT` has higher precedence than `UNION` and `EXCEPT` + (e.g., `A UNION B INTERSECT C` is equivalent to `A UNION (B INTERSECT C)`). + - Evaluation is left-to-right + (e.g., `A INTERSECT B INTERSECT C` is equivalent to `(A INTERSECT B) INTERSECT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 2.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct common device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 +INTERSECT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +INTERSECT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| null| +| 100| 90.0| +| 100| 85.0| ++---------+-----------+ +Total line number = 5 +It costs 0.087s +``` + +2. Get all common device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +INTERSECT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| ++---------+-----------+ +Total line number = 6 +It costs 0.139s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`EXCEPT`, use parentheses to explicitly specify precedence + > (e.g., `A INTERSECT (B UNION C)`). + + +## 3. EXCEPT +### 3.1 Overview +The EXCEPT operator returns rows that exist in the first result set but **not** in the second (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 3.2 Syntax +```sql +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`EXCEPT` or `EXCEPT DISTINCT`): Automatically removes duplicate rows. + - `EXCEPT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `EXCEPT` has the same precedence as `UNION`, and lower precedence than `INTERSECT` + (e.g., `A INTERSECT B EXCEPT C` is equivalent to `(A INTERSECT B) EXCEPT C`). + - Evaluation is left-to-right + (e.g., `A EXCEPT B EXCEPT C` is equivalent to `(A EXCEPT B) EXCEPT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 3.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct records from `table1` that do not exist in `table2` +```sql +SELECT device_id, temperature FROM table1 +EXCEPT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +EXCEPT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| null| +| 100| 88.0| ++---------+-----------+ +Total line number = 2 +It costs 0.173s +``` + +2. Get all records from `table1` that do not exist in `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +EXCEPT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| +| 101| null| +| 101| null| +| 101| null| +| 101| null| ++---------+-----------+ +Total line number = 12 +It costs 0.155s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`INTERSECT`, use parentheses to explicitly specify precedence + > (e.g., `A EXCEPT (B INTERSECT C)`). \ No newline at end of file diff --git a/src/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md b/src/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md new file mode 100644 index 000000000..3628b15ec --- /dev/null +++ b/src/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md @@ -0,0 +1,295 @@ + +# Set Operations + +IoTDB natively supports standard SQL set operations, including three core operators: **UNION**, **INTERSECT**, and **EXCEPT**. These operations enable seamless merging, comparison, and filtering of query results from multiple time-series data sources, greatly improving the flexibility and efficiency of time-series data analysis. + +> Note: This feature is available since version 2.0.9.1. + +## 1. UNION +### 1.1 Overview +The UNION operator combines all rows from two result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 1.2 Syntax +```sql +query UNION (ALL | DISTINCT) query +``` + +**Description** +1. **Duplicate Handling** + - Default (`UNION` or `UNION DISTINCT`): Automatically removes duplicate rows. + - `UNION ALL`: Preserves all rows (including duplicates) with higher performance. + +2. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types: + - Numeric compatibility: `INT32`, `INT64`, `FLOAT`, and `DOUBLE` are fully compatible with each other. + - String compatibility: `TEXT` and `STRING` are fully compatible. + - Special rule: `INT64` is compatible with `TIMESTAMP`. + +3. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 1.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct non-null device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION DISTINCT +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. Get all non-null device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 WHERE temperature IS NOT NULL +UNION ALL +SELECT device_id, temperature FROM table2 WHERE temperature IS NOT NULL; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> **Notes** +> - Set operations **do not guarantee result order**; actual output may differ from examples. + + +## 2. INTERSECT +### 2.1 Overview +The INTERSECT operator returns rows that exist in both result sets (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 2.2 Syntax +```sql +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`INTERSECT` or `INTERSECT DISTINCT`): Automatically removes duplicate rows. + - `INTERSECT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `INTERSECT` has higher precedence than `UNION` and `EXCEPT` + (e.g., `A UNION B INTERSECT C` is equivalent to `A UNION (B INTERSECT C)`). + - Evaluation is left-to-right + (e.g., `A INTERSECT B INTERSECT C` is equivalent to `(A INTERSECT B) INTERSECT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 2.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct common device and temperature records from `table1` and `table2` +```sql +SELECT device_id, temperature FROM table1 +INTERSECT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +INTERSECT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| null| +| 100| 90.0| +| 100| 85.0| ++---------+-----------+ +Total line number = 5 +It costs 0.087s +``` + +2. Get all common device and temperature records from `table1` and `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +INTERSECT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| ++---------+-----------+ +Total line number = 6 +It costs 0.139s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`EXCEPT`, use parentheses to explicitly specify precedence + > (e.g., `A INTERSECT (B UNION C)`). + + +## 3. EXCEPT +### 3.1 Overview +The EXCEPT operator returns rows that exist in the first result set but **not** in the second (order not guaranteed), supporting both duplicate elimination (default) and duplicate retention modes. + +### 3.2 Syntax +```sql +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +**Description** +1. **Duplicate Handling** + - Default (`EXCEPT` or `EXCEPT DISTINCT`): Automatically removes duplicate rows. + - `EXCEPT ALL`: Preserves duplicate rows, with slightly lower performance. + +2. **Precedence Rules** + - `EXCEPT` has the same precedence as `UNION`, and lower precedence than `INTERSECT` + (e.g., `A INTERSECT B EXCEPT C` is equivalent to `(A INTERSECT B) EXCEPT C`). + - Evaluation is left-to-right + (e.g., `A EXCEPT B EXCEPT C` is equivalent to `(A EXCEPT B) EXCEPT C`). + +3. **Input Requirements** + - The two queries must return the same number of columns. + - Corresponding columns must have compatible data types (same rules as UNION). + - NULL values are treated as equal (`NULL IS NOT DISTINCT FROM NULL`). + - If the `time` column is not included in `SELECT`, it does not participate in comparison and will not appear in the result. + +4. **Result Set Rules** + - Column names and order are inherited from the first query. + +### 3.3 Examples +Using the [sample data](../Reference/Sample-Data.md): + +1. Get distinct records from `table1` that do not exist in `table2` +```sql +SELECT device_id, temperature FROM table1 +EXCEPT +SELECT device_id, temperature FROM table2; + +-- Equivalent to: +SELECT device_id, temperature FROM table1 +EXCEPT DISTINCT +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| null| +| 100| 88.0| ++---------+-----------+ +Total line number = 2 +It costs 0.173s +``` + +2. Get all records from `table1` that do not exist in `table2` (including duplicates) +```sql +SELECT device_id, temperature FROM table1 +EXCEPT ALL +SELECT device_id, temperature FROM table2; +``` + +Result: +``` ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 100| null| +| 101| 85.0| +| 101| 85.0| +| 101| 90.0| +| 101| null| +| 101| null| +| 101| null| +| 101| null| ++---------+-----------+ +Total line number = 12 +It costs 0.155s +``` + +> **Notes** +> - Set operations **do not guarantee result order**. +> - When mixed with `UNION`/`INTERSECT`, use parentheses to explicitly specify precedence + > (e.g., `A EXCEPT (B INTERSECT C)`). \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md b/src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md new file mode 100644 index 000000000..dc7bc961f --- /dev/null +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_apache.md @@ -0,0 +1,322 @@ + + +# 集合操作 + +IoTDB 原生支持 SQL 标准集合操作,包括 UNION(并集)、INTERSECT(交集)和EXCEPT(差集)三种核心运算符。通过执行这些操作,可实现无缝合并、比较和筛选多源时序数据查询结果,显著提升时序数据分析的灵活性与效率。 + +> 注意:该功能从 V2.0.9-beta 版本开始提供。 + +## 1. UNION +### 1.1 概述 + +UNION 操作将两个查询结果集的所有行合并(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 1.2 语法定义 + +```SQL +query UNION (ALL | DISTINCT) query +``` + +**说明:** + +1. **去重规则:** + + 1. 默认(`UNION` 或 `UNION DISTINCT`):自动去除重复行。 + 2. `UNION ALL`:保留所有行(包括重复项),性能更高。 +2. **输入要求:** + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容,兼容性规则如下: + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 +3. **结果集规则:** + + 1. 列名及顺序继承第一个查询的定义。 + +### 1.3 使用示例 + +以[示例数据](../Reference/Sample-Data.md)为原始数据。 + +1. 获取 table1 和 table2 中设备及温度的非空数据集合(去重) + +```SQL +select device_id,temperature from table1 where temperature is not null +union +select device_id,temperature from table2 where temperature is not null; + +--等价于; +select device_id,temperature from table1 where temperature is not null +union distinct +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```Bash ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. 获取 table1 和 table2 中设备及温度的非空数据集合(保留重复) + +```SQL +select device_id,temperature from table1 where temperature is not null +union all +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```SQL ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 + +## 2. INTERSECT +### 2.1 概述 + +INTERSECT 操作返回两个查询结果集中共同存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 2.2 语法定义 + +```SQL +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`INTERSECT` 或 `INTERSECT DISTINCT`):自动去除重复行。 + 2. `INTERSECT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `INTERSECT` 优先级高于 `UNION` 和 `EXCEPT`(如 `A UNION B INTERSECT C` 等价于 `A UNION (B INTERSECT C)`)。 + 2. 从左到右计算(`A INTERSECT B INTERSECT C` 等价于 `(A INTERSECT B) INTERSECT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 2.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 和 table2 中设备及温度的共同数据(去重) + + ```SQL + select device_id, temperature from table1 + intersect + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + intersect distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| 90.0| + | 101| 85.0| + | 100| null| + | 100| 90.0| + | 100| 85.0| + +---------+-----------+ + Total line number = 5 + It costs 0.087s + ``` +2. 获取 table1 和 table2 中设备及温度的共同数据(保留重复) + + ```SQL + select device_id, temperature from table1 + intersect all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + +---------+-----------+ + Total line number = 6 + It costs 0.139s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`EXCEPT` 混合使用时,需通过括号明确优先级(如 `A INTERSECT (B UNION C)`)。 + +## 3. EXCEPT +### 3.1 概述 + +EXCEPT 操作返回第一个查询结果集存在但第二个查询结果集中不存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 3.2 语法定义 + +```SQL +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`EXCEPT` 或 `EXCEPT DISTINCT`):自动去除重复行。 + 2. `EXCEPT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `EXCEPT` 与 `UNION` 优先级相同,低于 `INTERSECT`(如 `A INTERSECT B EXCEPT C` 等价于 `(A INTERSECT B) EXCEPT C`)。 + 2. 从左到右计算(`A EXCEPT B EXCEPT C` 等价于 `(A EXCEPT B) EXCEPT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 3.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 中存在但 table2 中不存在的设备及温度数据(去重) + + ```SQL + select device_id, temperature from table1 + except + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + except distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| null| + | 100| 88.0| + +---------+-----------+ + Total line number = 2 + It costs 0.173s + ``` +2. 获取 table1 中存在但 table2 中不存在的设备及温度数据(保留重复) + + ```SQL + select device_id, temperature from table1 + except all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 88.0| + | 100| 90.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + | 101| null| + | 101| null| + | 101| null| + | 101| null| + +---------+-----------+ + Total line number = 12 + It costs 0.155s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`INTERSECT` 混合使用时,需通过括号明确优先级(如 `A EXCEPT (B INTERSECT C)`)。 diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md b/src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md new file mode 100644 index 000000000..07d8ff1fb --- /dev/null +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/Set-Operations_timecho.md @@ -0,0 +1,322 @@ + + +# 集合操作 + +IoTDB 原生支持 SQL 标准集合操作,包括 UNION(并集)、INTERSECT(交集)和EXCEPT(差集)三种核心运算符。通过执行这些操作,可实现无缝合并、比较和筛选多源时序数据查询结果,显著提升时序数据分析的灵活性与效率。 + +> 注意:该功能从 V2.0.9.1 版本开始提供。 + +## 1. UNION +### 1.1 概述 + +UNION 操作将两个查询结果集的所有行合并(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 1.2 语法定义 + +```SQL +query UNION (ALL | DISTINCT) query +``` + +**说明:** + +1. **去重规则:** + + 1. 默认(`UNION` 或 `UNION DISTINCT`):自动去除重复行。 + 2. `UNION ALL`:保留所有行(包括重复项),性能更高。 +2. **输入要求:** + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容,兼容性规则如下: + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 +3. **结果集规则:** + + 1. 列名及顺序继承第一个查询的定义。 + +### 1.3 使用示例 + +以[示例数据](../Reference/Sample-Data.md)为原始数据。 + +1. 获取 table1 和 table2 中设备及温度的非空数据集合(去重) + +```SQL +select device_id,temperature from table1 where temperature is not null +union +select device_id,temperature from table2 where temperature is not null; + +--等价于; +select device_id,temperature from table1 where temperature is not null +union distinct +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```Bash ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. 获取 table1 和 table2 中设备及温度的非空数据集合(保留重复) + +```SQL +select device_id,temperature from table1 where temperature is not null +union all +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```SQL ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 + +## 2. INTERSECT +### 2.1 概述 + +INTERSECT 操作返回两个查询结果集中共同存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 2.2 语法定义 + +```SQL +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`INTERSECT` 或 `INTERSECT DISTINCT`):自动去除重复行。 + 2. `INTERSECT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `INTERSECT` 优先级高于 `UNION` 和 `EXCEPT`(如 `A UNION B INTERSECT C` 等价于 `A UNION (B INTERSECT C)`)。 + 2. 从左到右计算(`A INTERSECT B INTERSECT C` 等价于 `(A INTERSECT B) INTERSECT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 2.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 和 table2 中设备及温度的共同数据(去重) + + ```SQL + select device_id, temperature from table1 + intersect + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + intersect distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| 90.0| + | 101| 85.0| + | 100| null| + | 100| 90.0| + | 100| 85.0| + +---------+-----------+ + Total line number = 5 + It costs 0.087s + ``` +2. 获取 table1 和 table2 中设备及温度的共同数据(保留重复) + + ```SQL + select device_id, temperature from table1 + intersect all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + +---------+-----------+ + Total line number = 6 + It costs 0.139s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`EXCEPT` 混合使用时,需通过括号明确优先级(如 `A INTERSECT (B UNION C)`)。 + +## 3. EXCEPT +### 3.1 概述 + +EXCEPT 操作返回第一个查询结果集存在但第二个查询结果集中不存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 3.2 语法定义 + +```SQL +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`EXCEPT` 或 `EXCEPT DISTINCT`):自动去除重复行。 + 2. `EXCEPT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `EXCEPT` 与 `UNION` 优先级相同,低于 `INTERSECT`(如 `A INTERSECT B EXCEPT C` 等价于 `(A INTERSECT B) EXCEPT C`)。 + 2. 从左到右计算(`A EXCEPT B EXCEPT C` 等价于 `(A EXCEPT B) EXCEPT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 3.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 中存在但 table2 中不存在的设备及温度数据(去重) + + ```SQL + select device_id, temperature from table1 + except + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + except distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| null| + | 100| 88.0| + +---------+-----------+ + Total line number = 2 + It costs 0.173s + ``` +2. 获取 table1 中存在但 table2 中不存在的设备及温度数据(保留重复) + + ```SQL + select device_id, temperature from table1 + except all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 88.0| + | 100| 90.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + | 101| null| + | 101| null| + | 101| null| + | 101| null| + +---------+-----------+ + Total line number = 12 + It costs 0.155s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`INTERSECT` 混合使用时,需通过括号明确优先级(如 `A EXCEPT (B INTERSECT C)`)。 diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md b/src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md new file mode 100644 index 000000000..dc7bc961f --- /dev/null +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_apache.md @@ -0,0 +1,322 @@ + + +# 集合操作 + +IoTDB 原生支持 SQL 标准集合操作,包括 UNION(并集)、INTERSECT(交集)和EXCEPT(差集)三种核心运算符。通过执行这些操作,可实现无缝合并、比较和筛选多源时序数据查询结果,显著提升时序数据分析的灵活性与效率。 + +> 注意:该功能从 V2.0.9-beta 版本开始提供。 + +## 1. UNION +### 1.1 概述 + +UNION 操作将两个查询结果集的所有行合并(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 1.2 语法定义 + +```SQL +query UNION (ALL | DISTINCT) query +``` + +**说明:** + +1. **去重规则:** + + 1. 默认(`UNION` 或 `UNION DISTINCT`):自动去除重复行。 + 2. `UNION ALL`:保留所有行(包括重复项),性能更高。 +2. **输入要求:** + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容,兼容性规则如下: + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 +3. **结果集规则:** + + 1. 列名及顺序继承第一个查询的定义。 + +### 1.3 使用示例 + +以[示例数据](../Reference/Sample-Data.md)为原始数据。 + +1. 获取 table1 和 table2 中设备及温度的非空数据集合(去重) + +```SQL +select device_id,temperature from table1 where temperature is not null +union +select device_id,temperature from table2 where temperature is not null; + +--等价于; +select device_id,temperature from table1 where temperature is not null +union distinct +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```Bash ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. 获取 table1 和 table2 中设备及温度的非空数据集合(保留重复) + +```SQL +select device_id,temperature from table1 where temperature is not null +union all +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```SQL ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 + +## 2. INTERSECT +### 2.1 概述 + +INTERSECT 操作返回两个查询结果集中共同存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 2.2 语法定义 + +```SQL +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`INTERSECT` 或 `INTERSECT DISTINCT`):自动去除重复行。 + 2. `INTERSECT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `INTERSECT` 优先级高于 `UNION` 和 `EXCEPT`(如 `A UNION B INTERSECT C` 等价于 `A UNION (B INTERSECT C)`)。 + 2. 从左到右计算(`A INTERSECT B INTERSECT C` 等价于 `(A INTERSECT B) INTERSECT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 2.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 和 table2 中设备及温度的共同数据(去重) + + ```SQL + select device_id, temperature from table1 + intersect + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + intersect distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| 90.0| + | 101| 85.0| + | 100| null| + | 100| 90.0| + | 100| 85.0| + +---------+-----------+ + Total line number = 5 + It costs 0.087s + ``` +2. 获取 table1 和 table2 中设备及温度的共同数据(保留重复) + + ```SQL + select device_id, temperature from table1 + intersect all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + +---------+-----------+ + Total line number = 6 + It costs 0.139s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`EXCEPT` 混合使用时,需通过括号明确优先级(如 `A INTERSECT (B UNION C)`)。 + +## 3. EXCEPT +### 3.1 概述 + +EXCEPT 操作返回第一个查询结果集存在但第二个查询结果集中不存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 3.2 语法定义 + +```SQL +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`EXCEPT` 或 `EXCEPT DISTINCT`):自动去除重复行。 + 2. `EXCEPT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `EXCEPT` 与 `UNION` 优先级相同,低于 `INTERSECT`(如 `A INTERSECT B EXCEPT C` 等价于 `(A INTERSECT B) EXCEPT C`)。 + 2. 从左到右计算(`A EXCEPT B EXCEPT C` 等价于 `(A EXCEPT B) EXCEPT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 3.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 中存在但 table2 中不存在的设备及温度数据(去重) + + ```SQL + select device_id, temperature from table1 + except + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + except distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| null| + | 100| 88.0| + +---------+-----------+ + Total line number = 2 + It costs 0.173s + ``` +2. 获取 table1 中存在但 table2 中不存在的设备及温度数据(保留重复) + + ```SQL + select device_id, temperature from table1 + except all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 88.0| + | 100| 90.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + | 101| null| + | 101| null| + | 101| null| + | 101| null| + +---------+-----------+ + Total line number = 12 + It costs 0.155s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`INTERSECT` 混合使用时,需通过括号明确优先级(如 `A EXCEPT (B INTERSECT C)`)。 diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md b/src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md new file mode 100644 index 000000000..07d8ff1fb --- /dev/null +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/Set-Operations_timecho.md @@ -0,0 +1,322 @@ + + +# 集合操作 + +IoTDB 原生支持 SQL 标准集合操作,包括 UNION(并集)、INTERSECT(交集)和EXCEPT(差集)三种核心运算符。通过执行这些操作,可实现无缝合并、比较和筛选多源时序数据查询结果,显著提升时序数据分析的灵活性与效率。 + +> 注意:该功能从 V2.0.9.1 版本开始提供。 + +## 1. UNION +### 1.1 概述 + +UNION 操作将两个查询结果集的所有行合并(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 1.2 语法定义 + +```SQL +query UNION (ALL | DISTINCT) query +``` + +**说明:** + +1. **去重规则:** + + 1. 默认(`UNION` 或 `UNION DISTINCT`):自动去除重复行。 + 2. `UNION ALL`:保留所有行(包括重复项),性能更高。 +2. **输入要求:** + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容,兼容性规则如下: + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 +3. **结果集规则:** + + 1. 列名及顺序继承第一个查询的定义。 + +### 1.3 使用示例 + +以[示例数据](../Reference/Sample-Data.md)为原始数据。 + +1. 获取 table1 和 table2 中设备及温度的非空数据集合(去重) + +```SQL +select device_id,temperature from table1 where temperature is not null +union +select device_id,temperature from table2 where temperature is not null; + +--等价于; +select device_id,temperature from table1 where temperature is not null +union distinct +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```Bash ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 88.0| ++---------+-----------+ +Total line number = 5 +It costs 0.074s +``` + +2. 获取 table1 和 table2 中设备及温度的非空数据集合(保留重复) + +```SQL +select device_id,temperature from table1 where temperature is not null +union all +select device_id,temperature from table2 where temperature is not null; +``` + +执行结果: + +```SQL ++---------+-----------+ +|device_id|temperature| ++---------+-----------+ +| 101| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 101| 85.0| +| 100| 90.0| +| 100| 85.0| +| 100| 85.0| +| 100| 88.0| +| 100| 90.0| +| 100| 90.0| +| 101| 90.0| +| 101| 85.0| +| 101| 85.0| +| 100| 85.0| +| 100| 90.0| ++---------+-----------+ +Total line number = 17 +It costs 0.108s +``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 + +## 2. INTERSECT +### 2.1 概述 + +INTERSECT 操作返回两个查询结果集中共同存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 2.2 语法定义 + +```SQL +query1 INTERSECT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`INTERSECT` 或 `INTERSECT DISTINCT`):自动去除重复行。 + 2. `INTERSECT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `INTERSECT` 优先级高于 `UNION` 和 `EXCEPT`(如 `A UNION B INTERSECT C` 等价于 `A UNION (B INTERSECT C)`)。 + 2. 从左到右计算(`A INTERSECT B INTERSECT C` 等价于 `(A INTERSECT B) INTERSECT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 2.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 和 table2 中设备及温度的共同数据(去重) + + ```SQL + select device_id, temperature from table1 + intersect + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + intersect distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| 90.0| + | 101| 85.0| + | 100| null| + | 100| 90.0| + | 100| 85.0| + +---------+-----------+ + Total line number = 5 + It costs 0.087s + ``` +2. 获取 table1 和 table2 中设备及温度的共同数据(保留重复) + + ```SQL + select device_id, temperature from table1 + intersect all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + +---------+-----------+ + Total line number = 6 + It costs 0.139s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`EXCEPT` 混合使用时,需通过括号明确优先级(如 `A INTERSECT (B UNION C)`)。 + +## 3. EXCEPT +### 3.1 概述 + +EXCEPT 操作返回第一个查询结果集存在但第二个查询结果集中不存在的行(不保证结果顺序),支持去重(默认)和保留重复两种模式。 + +### 3.2 语法定义 + +```SQL +query1 EXCEPT [ALL | DISTINCT] query2 +``` + +​**说明**​: + +1. ​**去重规则**​: + + 1. 默认(`EXCEPT` 或 `EXCEPT DISTINCT`):自动去除重复行。 + 2. `EXCEPT ALL`:保留所有重复行(包括重复项),性能略低。 +2. ​**优先级规则**​: + + 1. `EXCEPT` 与 `UNION` 优先级相同,低于 `INTERSECT`(如 `A INTERSECT B EXCEPT C` 等价于 `(A INTERSECT B) EXCEPT C`)。 + 2. 从左到右计算(`A EXCEPT B EXCEPT C` 等价于 `(A EXCEPT B) EXCEPT C`)。 +3. ​**输入要求**​: + + 1. 两个查询结果的列数必须相同。 + 2. 对应列数据类型需兼容(兼容性规则同 UNION): + * 数值类型互容:`INT32`、`INT64`、`FLOAT`、`DOUBLE` 之间完全兼容。 + * 字符串类型互容:`TEXT` 与 `STRING` 完全兼容。 + * 特殊规则:`INT64` 与 `TIMESTAMP` 兼容。 + 3. NULL 值视为相等(`NULL IS NOT DISTINCT FROM NULL`)。 + 4. 若 `SELECT` 未包含 `time` 列,则 `time` 列不参与比较,结果集无 `time` 列。 +4. ​**结果集规则**​: + + 1. 列名及顺序继承第一个查询的定义。 + +### 3.3 使用示例 + +基于 [示例数据](../Reference/Sample-Data.md): + +1. 获取 table1 中存在但 table2 中不存在的设备及温度数据(去重) + + ```SQL + select device_id, temperature from table1 + except + select device_id, temperature from table2; + + --等价于; + select device_id, temperature from table1 + except distinct + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 101| null| + | 100| 88.0| + +---------+-----------+ + Total line number = 2 + It costs 0.173s + ``` +2. 获取 table1 中存在但 table2 中不存在的设备及温度数据(保留重复) + + ```SQL + select device_id, temperature from table1 + except all + select device_id, temperature from table2; + ``` + + 执行结果: + + ```Bash + +---------+-----------+ + |device_id|temperature| + +---------+-----------+ + | 100| 85.0| + | 100| 88.0| + | 100| 90.0| + | 100| 90.0| + | 100| null| + | 101| 85.0| + | 101| 85.0| + | 101| 90.0| + | 101| null| + | 101| null| + | 101| null| + | 101| null| + +---------+-----------+ + Total line number = 12 + It costs 0.155s + ``` + +> ​**注意**​: +> +> * 集合操作​**不保证结果顺序**​,实际输出顺序可能与示例不同。 +> * 与 `UNION`/`INTERSECT` 混合使用时,需通过括号明确优先级(如 `A EXCEPT (B INTERSECT C)`)。