Join Queries
Join Concepts
Driving Table
The table that drives the join query. In the Left Join series, the left table is the driving table, and in the Right Join series, the right table is the driving table.
Join Conditions
Join conditions refer to the conditions specified for table joins. All join queries supported by TDengine require specifying join conditions, which typically (except for Inner Join and Window Join) appear after ON
. Semantically, conditions that appear after WHERE
in an Inner Join can also be considered join conditions, while Window Join specifies join conditions through WINDOW_OFFSET
.
Except for ASOF Join, all Join types supported by TDengine must explicitly specify join conditions. ASOF Join has implicit default join conditions, so it is not necessary to specify them explicitly if the default conditions are sufficient.
Apart from ASOF/Window Join, join conditions can include any number of additional conditions besides the main join condition, which must be related by AND
with the main join condition, while there is no such restriction between other conditions. These additional conditions can include any logical combination of primary key columns, Tags, ordinary columns, constants, and their scalar functions or operations.
For example, with smart meters, the following SQL statements all contain valid join conditions:
SELECT a.* FROM meters a LEFT JOIN meters b ON a.ts = b.ts AND a.ts > '2023-10-18 10:00:00.000';
SELECT a.* FROM meters a LEFT JOIN meters b ON a.ts = b.ts AND (a.ts > '2023-10-18 10:00:00.000' OR a.ts < '2023-10-17 10:00:00.000');
SELECT a.* FROM meters a LEFT JOIN meters b ON timetruncate(a.ts, 1s) = timetruncate(b.ts, 1s) AND (a.ts + 1s > '2023-10-18 10:00:00.000' OR a.groupId > 0);
SELECT a.* FROM meters a LEFT ASOF JOIN meters b ON timetruncate(a.ts, 1s) < timetruncate(b.ts, 1s) AND a.groupId = b.groupId;
Main Join Condition
As a time-series database, all join queries in TDengine revolve around the primary key timestamp column. Therefore, all join queries (except ASOF/Window Join) must include an equality condition on the primary key column, and the first primary key column equality condition that appears in the join conditions will be considered the main join condition. ASOF Join's main join condition can include non-equality conditions, while Window Join's main join condition is specified through WINDOW_OFFSET
.
Apart from Window Join, TDengine supports the timetruncate
function operation in the main join condition, such as ON timetruncate(a.ts, 1s) = timetruncate(b.ts, 1s)
, but does not support other functions and scalar operations.
Grouping Conditions
The characteristic ASOF/Window Join of time-series databases supports grouping the input data of join queries and then performing join operations within each group. Grouping only affects the input of the join query, and the output results will not include group information. Equality conditions that appear after ON
in ASOF/Window Join (except for ASOF's main join condition) will be considered as grouping conditions.
Primary Key Timeline
As a time-series database, TDengine requires each table (subtable) to have a primary key timestamp column, which will serve as the primary key timeline for many time-related operations. The result of a subquery or the result of a Join operation also needs to clearly identify which column will be considered the primary key timeline for subsequent time-related operations. In subqueries, the first appearing ordered primary key column (or its operation) or a pseudocolumn equivalent to the primary key column (_wstart
/_wend
) will be considered the primary key timeline of the output table. The selection of the primary key timeline in Join output results follows these rules:
- In the Left/Right Join series, the primary key column of the driving table (subquery) will be used as the primary key timeline for subsequent queries; additionally, within the Window Join window, since both tables are ordered, any table's primary key column can be used as the primary key timeline, with a preference for the primary key column of the same table.
- Inner Join can use the primary key column of any table as the primary key timeline, but when there are grouping conditions similar to tag column equality conditions related by
AND
with the main join condition, it will not produce a primary key timeline. - Full Join cannot produce any valid primary key sequence, thus it has no primary key timeline, which means that time-related operations cannot be performed in Full Join.
Syntax Explanation
In the following sections, the Left/Right Join series will be introduced in a shared manner. Therefore, subsequent introductions including Outer, Semi, Anti-Semi, ASOF, and Window series all adopt a similar "left/right" notation to introduce Left/Right Join simultaneously. Here is a brief explanation of this notation: the part written before "/" applies to Left Join, while the part written after "/" applies to Right Join.
For example:
"left/right table" means for Left Join, it refers to "left table", and for Right Join, it refers to "right table";
Similarly,
"right/left table" means for Left Join, it refers to "right table", and for Right Join, it refers to "left table";
Join Features
Inner Join
Definition
Inner Join - Only the data that meets the join conditions in both the left and right tables will be returned, which can be seen as the intersection of data that meets the join conditions from both tables.
Syntax
SELECT ... FROM table_name1 [INNER] JOIN table_name2 [ON ...] [WHERE ...] [...]
or
SELECT ... FROM table_name1, table_name2 WHERE ... [...]
Result Set
The Cartesian product set of rows from the left and right tables that meet the join conditions.
Applicable Scope
Supports Inner Join between supertables, basic tables, subtables, and subqueries.
Explanation
- For the first syntax, the
INNER
keyword is optional,ON
and/orWHERE
can specify the main join conditions and other join conditions, andWHERE
can also specify filter conditions. At least one ofON
/WHERE
must be specified. - For the second syntax, you can specify the main join conditions, other join conditions, and filter conditions in
WHERE
. - When performing an Inner Join on supertables, the tag column equality conditions related to the main join condition
AND
will be used as grouping conditions, so the output results cannot be guaranteed to be ordered.
Example
Moments when both table d1001 and table d1002 have voltages greater than 220V and their respective voltage values:
SELECT a.ts, a.voltage, b.voltage FROM d1001 a JOIN d1002 b ON a.ts = b.ts and a.voltage > 220 and b.voltage > 220