diff --git a/src/.vuepress/sidebar/V2.0.x/en-Tree.ts b/src/.vuepress/sidebar/V2.0.x/en-Tree.ts index f2bc7aa80..ca17a3aec 100644 --- a/src/.vuepress/sidebar/V2.0.x/en-Tree.ts +++ b/src/.vuepress/sidebar/V2.0.x/en-Tree.ts @@ -249,7 +249,7 @@ export const enSidebar = { children: [ { text: 'Identifiers', link: 'Syntax-Rule' }, { text: 'Keywords', link: 'Keywords' }, - { text: 'SQL Manual', link: 'SQL-Manual' }, + { text: 'SQL Manual', link: 'SQL-Manual_apache' }, { text: 'Functions and Operators', collapsible: true, diff --git a/src/.vuepress/sidebar/V2.0.x/zh-Tree.ts b/src/.vuepress/sidebar/V2.0.x/zh-Tree.ts index de1e45c8b..b50ba2a05 100644 --- a/src/.vuepress/sidebar/V2.0.x/zh-Tree.ts +++ b/src/.vuepress/sidebar/V2.0.x/zh-Tree.ts @@ -238,7 +238,7 @@ export const zhSidebar = { children: [ { text: '标识符', link: 'Syntax-Rule' }, { text: '关键字', link: 'Keywords' }, - { text: 'SQL手册', link: 'SQL-Manual' }, + { text: 'SQL手册', link: 'SQL-Manual_apache' }, { text: '函数与运算符', collapsible: true, diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/en-Tree.ts b/src/.vuepress/sidebar_timecho/V2.0.x/en-Tree.ts index 12b35c957..d21d05a66 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/en-Tree.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/en-Tree.ts @@ -267,7 +267,7 @@ export const enSidebar = { children: [ { text: 'Identifiers', link: 'Syntax-Rule' }, { text: 'Keywords', link: 'Keywords' }, - { text: 'SQL Manual', link: 'SQL-Manual' }, + { text: 'SQL Manual', link: 'SQL-Manual_timecho' }, { text: 'Functions and Operators', collapsible: true, diff --git a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Tree.ts b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Tree.ts index f86cb1b31..40db696a4 100644 --- a/src/.vuepress/sidebar_timecho/V2.0.x/zh-Tree.ts +++ b/src/.vuepress/sidebar_timecho/V2.0.x/zh-Tree.ts @@ -250,7 +250,7 @@ export const zhSidebar = { children: [ { text: '标识符', link: 'Syntax-Rule' }, { text: '关键字', link: 'Keywords' }, - { text: 'SQL手册', link: 'SQL-Manual' }, + { text: 'SQL手册', link: 'SQL-Manual_timecho' }, { text: '函数与运算符', collapsible: true, diff --git a/src/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md b/src/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md index 6a7d90b4d..8e989d2ed 100644 --- a/src/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md +++ b/src/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md @@ -349,247 +349,10 @@ IoTDB> show devices All devices will definitely have a TTL, meaning it cannot be null. INF represents infinity. -## 2. Device Template -IoTDB supports the device template function, enabling different entities of the same type to share metadata, reduce the memory usage of metadata, and simplify the management of numerous entities and measurements. +## 2. Timeseries Management - -### 2.1 Create Device Template - -The SQL syntax for creating a metadata template is as follows: - -```sql -CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' -``` - -**Example 1:** Create a template containing two non-aligned timeseries - -```shell -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) -``` - -**Example 2:** Create a template containing a group of aligned timeseries - -```shell -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` - -The` lat` and `lon` measurements are aligned. - -When creating a template, the system will automatically assign default encoding and compression methods, requiring no manual specification. If your business scenario requires custom adjustments, you may refer to the following example: - -```shell -IoTDB> create device template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY) -``` -For a full list of supported data types and corresponding encoding methods, please refer to [Compression & Encoding](../Technical-Insider/Encoding-and-Compression.md)。 - -![](/img/%E6%A8%A1%E6%9D%BF.png) - -![](/img/templateEN.jpg) - -### 2.2 Set Device Template - -After a device template is created, it should be set to specific path before creating related timeseries or insert data. - -**It should be ensured that the related database has been set before setting template.** - -**It is recommended to set device template to database path. It is not suggested to set device template to some path above database** - -**It is forbidden to create timeseries under a path setting s tedeviceplate. Device template shall not be set on a prefix path of an existing timeseries.** - -The SQL Statement for setting device template is as follow: - -```shell -IoTDB> set device template t1 to root.sg1.d1 -``` - -### 2.3 Activate Device Template - -After setting the device template, with the system enabled to auto create schema, you can insert data into the timeseries. For example, suppose there's a database root.sg1 and t1 has been set to root.sg1.d1, then timeseries like root.sg1.d1.temperature and root.sg1.d1.status are available and data points can be inserted. - - -**Attention**: Before inserting data or the system not enabled to auto create schema, timeseries defined by the device template will not be created. You can use the following SQL statement to create the timeseries or activate the templdeviceate, act before inserting data: - -```shell -IoTDB> create timeseries using device template on root.sg1.d1 -``` - -**Example:** Execute the following statement - -```shell -IoTDB> set device template t1 to root.sg1.d1 -IoTDB> set device template t2 to root.sg1.d2 -IoTDB> create timeseries using device template on root.sg1.d1 -IoTDB> create timeseries using device template on root.sg1.d2 -``` - -Show the time series: - -```sql -show timeseries root.sg1.** -```` - -```shell -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression|tags|attributes|deadband|deadband parameters| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -|root.sg1.d1.temperature| null| root.sg1| FLOAT| RLE| SNAPPY|null| null| null| null| -| root.sg1.d1.status| null| root.sg1| BOOLEAN| PLAIN| SNAPPY|null| null| null| null| -| root.sg1.d2.lon| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -| root.sg1.d2.lat| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -``` - -Show the devices: - -```sql -show devices root.sg1.** -```` - -```shell -+---------------+---------+ -| devices|isAligned| -+---------------+---------+ -| root.sg1.d1| false| -| root.sg1.d2| true| -+---------------+---------+ -```` - -### 2.4 Show Device Template - -- Show all device templates - -The SQL statement looks like this: - -```shell -IoTDB> show device templates -``` - -The execution result is as follows: - -```shell -+-------------+ -|template name| -+-------------+ -| t2| -| t1| -+-------------+ -``` - -- Show nodes under in device template - -The SQL statement looks like this: - -```shell -IoTDB> show nodes in device template t1 -``` - -The execution result is as follows: - -```shell -+-----------+--------+--------+-----------+ -|child nodes|dataType|encoding|compression| -+-----------+--------+--------+-----------+ -|temperature| FLOAT| RLE| SNAPPY| -| status| BOOLEAN| PLAIN| SNAPPY| -+-----------+--------+--------+-----------+ -``` - -- Show the path prefix where a device template is set - -```shell -IoTDB> show paths set device template t1 -``` - -The execution result is as follows: - -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -- Show the path prefix where a device template is used (i.e. the time series has been created) - -```shell -IoTDB> show paths using device template t1 -``` - -The execution result is as follows: - -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -### 2.5 Deactivate device Template - -To delete a group of timeseries represented by device template, namely deactivate the device template, use the following SQL statement: - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -``` - -or - -```shell -IoTDB> deactivate device template t1 from root.sg1.d1 -``` - -The deactivation supports batch process. - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -``` - -or - -```shell -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` - -If the template name is not provided in sql, all template activation on paths matched by given path pattern will be removed. - -### 2.6 Unset Device Template - -The SQL Statement for unsetting device template is as follow: - -```shell -IoTDB> unset device template t1 from root.sg1.d1 -``` - -**Attention**: It should be guaranteed that none of the timeseries represented by the target device template exists, before unset it. It can be achieved by deactivation operation. - -### 2.7 Drop Device Template - -The SQL Statement for dropping device template is as follow: - -```shell -IoTDB> drop device template t1 -``` - -**Attention**: Dropping an already set template is not supported. - -### 2.8 Alter Device Template - -In a scenario where measurements need to be added, you can modify the template to add measurements to all devicesdevice using the device template. - -The SQL Statement for altering device template is as follow: - -```shell -IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) -``` - -**When executing data insertion to devices with device template set on related prefix path and there are measurements not present in this device template, the measurements will be auto added to this device template.** - -## 3. Timeseries Management - -### 3.1 Create Timeseries +### 2.1 Create Timeseries According to the storage model selected before, we can create corresponding timeseries in the two databases respectively. The SQL statements for creating timeseries are as follows: @@ -629,7 +392,7 @@ error: encoding TS_2DIFF does not support BOOLEAN For a full list of supported data types and corresponding encoding methods, please refer to [Compression & Encoding](../Technical-Insider/Encoding-and-Compression.md)。 -### 3.2 Create Aligned Timeseries +### 2.2 Create Aligned Timeseries The SQL statement for creating a group of timeseries are as follows: @@ -641,7 +404,7 @@ You can set different datatype, encoding, and compression for the timeseries in It is also supported to set an alias, tag, and attribute for aligned timeseries. -### 3.3 Delete Timeseries +### 2.3 Delete Timeseries To delete the timeseries we created before, we are able to use `(DELETE | DROP) TimeSeries ` statement. @@ -654,7 +417,7 @@ IoTDB> delete timeseries root.ln.wf02.* IoTDB> drop timeseries root.ln.wf02.* ``` -### 3.4 Show Timeseries +### 2.4 Show Timeseries * SHOW LATEST? TIMESERIES pathPattern? whereClause? limitClause? @@ -798,7 +561,7 @@ It costs 0.004s It is worth noting that when the queried path does not exist, the system will return no timeseries. -### 3.5 Count Timeseries +### 2.5 Count Timeseries IoTDB is able to use `COUNT TIMESERIES ` to count the number of timeseries matching the path. SQL statements are as follows: @@ -883,7 +646,7 @@ It costs 0.002s > Note: The path of timeseries is just a filter condition, which has no relationship with the definition of level. -### 3.6 Active Timeseries Query +### 2.6 Active Timeseries Query By adding WHERE time filter conditions to the existing SHOW/COUNT TIMESERIES, we can obtain time series with data within the specified time range. It is important to note that in metadata queries with time filters, views are not considered; only the time series actually stored in the TsFile are taken into account. @@ -923,7 +686,7 @@ IoTDB> count timeseries where time >= 15000 and time < 16000; +-----------------+ ``` Regarding the definition of active time series, data that can be queried normally is considered active, meaning time series that have been inserted but deleted are not included. -### 3.7 Tag and Attribute Management +### 2.7 Tag and Attribute Management We can also add an alias, extra tag and attribute information while creating one timeseries. @@ -1098,9 +861,9 @@ IoTDB> show timeseries where TAGS(tag1)='v1' The above operations are supported for timeseries tag, attribute updates, etc. -## 4. Path query +## 3. Path query -### 4.1 Path +### 3.1 Path A `path` is an expression that conforms to the following constraints: @@ -1120,7 +883,7 @@ wildcard ; ``` -### 4.2 NodeName +### 3.2 NodeName - The parts of a path separated by `.` are called node names (`nodeName`). - For example, `root.a.b.c` is a path with a depth of 4 levels, where `root`, `a`, `b`, and `c` are all node names. @@ -1135,11 +898,11 @@ wildcard - UNICODE Chinese characters (`\u2E80` to `\u9FFF`) - **Case sensitivity**: On Windows systems, path node names in the database are case-insensitive. For example, `root.ln` and `root.LN` are considered the same path. -### 4.3 Special Characters (Backquote) +### 3.3 Special Characters (Backquote) If special characters (such as spaces or punctuation marks) are needed in a `nodeName`, you can enclose the node name in Backquote (`). For more information on the use of backticks, please refer to [Backquote](../SQL-Manual/Syntax-Rule.md#reverse-quotation-marks). -### 4.4 Path Pattern +### 3.4 Path Pattern To make it more convenient and efficient to express multiple time series, IoTDB provides paths with wildcards `*` and `**`. Wildcards can appear in any level of a path. @@ -1152,7 +915,7 @@ To make it more convenient and efficient to express multiple time series, IoTDB **Note**: `*` and `**` cannot be placed at the beginning of a path. -### 4.5 Show Child Paths +### 3.5 Show Child Paths ``` SHOW CHILD PATHS pathPattern @@ -1180,7 +943,7 @@ It costs 0.002s > get all paths in form of root.xx.xx.xx:show child paths root.xx.xx -### 4.6 Show Child Nodes +### 3.6 Show Child Nodes ``` SHOW CHILD NODES pathPattern @@ -1211,7 +974,7 @@ Example: +------------+ ``` -### 4.7 Count Nodes +### 3.7 Count Nodes IoTDB is able to use `COUNT NODES LEVEL=` to count the number of nodes at the given level in current Metadata Tree considering a given pattern. IoTDB will find paths that @@ -1264,7 +1027,7 @@ It costs 0.002s > Note: The path of timeseries is just a filter condition, which has no relationship with the definition of level. -### 4.8 Show Devices +### 3.8 Show Devices * SHOW DEVICES pathPattern? (WITH DATABASE)? devicesWhereClause? limitClause? @@ -1345,7 +1108,7 @@ Total line number = 2 It costs 0.001s ``` -### 4.9 Count Devices +### 3.9 Count Devices * COUNT DEVICES / @@ -1390,7 +1153,7 @@ Total line number = 1 It costs 0.004s ``` -### 4.10 Active Device Query +### 3.10 Active Device Query Similar to active timeseries query, we can add time filter conditions to device viewing and statistics to query active devices that have data within a certain time range. The definition of active here is the same as for active time series. An example usage is as follows: ``` IoTDB> insert into root.sg.data(timestamp, s1,s2) values(15000, 1, 2); diff --git a/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md b/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md index 67737449a..ab81bf055 100644 --- a/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md +++ b/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md @@ -1,3 +1,6 @@ +--- +redirectTo: SQL-Manual_apache.html +--- - -# SQL Manual - -## 1. DATABASE MANAGEMENT - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -### 1.1 Create Database - -```sql -IoTDB > create database root.ln -IoTDB > create database root.sgcc -``` - -### 1.2 Show Databases - -```sql -IoTDB> SHOW DATABASES -IoTDB> SHOW DATABASES root.** -``` - -### 1.3 Delete Database - -```sql -IoTDB > DELETE DATABASE root.ln -IoTDB > DELETE DATABASE root.sgcc -// delete all data, all timeseries and all databases -IoTDB > DELETE DATABASE root.** -``` - -### 1.4 Count Databases - -```sql -IoTDB> count databases -IoTDB> count databases root.* -IoTDB> count databases root.sgcc.* -IoTDB> count databases root.sgcc -``` - -### 1.5 Setting up heterogeneous databases (Advanced operations) - -#### Set heterogeneous parameters when creating a Database - -```sql -CREATE DATABASE root.db WITH SCHEMA_REPLICATION_FACTOR=1, DATA_REPLICATION_FACTOR=3, SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; -``` - -#### Adjust heterogeneous parameters at run time - -```sql -ALTER DATABASE root.db WITH SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; -``` - -#### Show heterogeneous databases - -```sql -SHOW DATABASES DETAILS -``` - -### 1.6 TTL - -#### Set TTL - -```sql -IoTDB> set ttl to root.ln 3600000 -IoTDB> set ttl to root.sgcc.** 3600000 -IoTDB> set ttl to root.** 3600000 -``` - -#### Unset TTL - -```sql -IoTDB> unset ttl from root.ln -IoTDB> unset ttl from root.sgcc.** -IoTDB> unset ttl from root.** -``` - -#### Show TTL - -```sql -IoTDB> SHOW ALL TTL -IoTDB> SHOW TTL ON StorageGroupNames -IoTDB> SHOW DEVICES -``` - -## 2. DEVICE TEMPLATE - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -![img](/img/%E6%A8%A1%E6%9D%BF.png) - - - - - -![img](/img/templateEN.jpg) - -### 2.1 Create Device Template - -**Example 1:** Create a template containing two non-aligned timeseires - -```shell -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) -``` - -**Example 2:** Create a template containing a group of aligned timeseires - -```shell -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` - -The` lat` and `lon` measurements are aligned. - -### 2.2 Set Device Template - -```sql -IoTDB> set device template t1 to root.sg1.d1 -``` - -### 2.3 Activate Device Template - -```sql -IoTDB> set device template t1 to root.sg1.d1 -IoTDB> set device template t2 to root.sg1.d2 -IoTDB> create timeseries using device template on root.sg1.d1 -IoTDB> create timeseries using device template on root.sg1.d2 -``` - -### 2.4 Show Device Template - -```sql -IoTDB> show device templates -IoTDB> show nodes in device template t1 -IoTDB> show paths set device template t1 -IoTDB> show paths using device template t1 -``` - -### 2.5 Deactivate Device Template - -```sql -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -IoTDB> deactivate device template t1 from root.sg1.d1 -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` - -### 2.6 Unset Device Template - -```sql -IoTDB> unset device template t1 from root.sg1.d1 -``` - -### 2.7 Drop Device Template - -```sql -IoTDB> drop device template t1 -``` - -### 2.8 Alter Device Template - -```sql -IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) -``` - -## 3. TIMESERIES MANAGEMENT - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -### 3.1 Create Timeseries - -```sql -IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT -IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT -IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT -``` - -- From v0.13, you can use a simplified version of the SQL statements to create timeseries: - -```sql -IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT -IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT -IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT -``` - -- Notice that when in the CREATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below: - -```sql -IoTDB > create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN -error: encoding TS_2DIFF does not support BOOLEAN -``` - -### 3.2 Create Aligned Timeseries - -```sql -IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT , longitude FLOAT) -``` - -### 3.3 Delete Timeseries - -```sql -IoTDB> delete timeseries root.ln.wf01.wt01.status -IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware -IoTDB> delete timeseries root.ln.wf02.* -IoTDB> drop timeseries root.ln.wf02.* -``` - -### 3.4 Show Timeseries - -```sql -IoTDB> show timeseries root.** -IoTDB> show timeseries root.ln.** -IoTDB> show timeseries root.ln.** limit 10 offset 10 -IoTDB> show timeseries root.ln.** where timeseries contains 'wf01.wt' -IoTDB> show timeseries root.ln.** where dataType=FLOAT -``` - -### 3.5 Count Timeseries - -```sql -IoTDB > COUNT TIMESERIES root.** -IoTDB > COUNT TIMESERIES root.ln.** -IoTDB > COUNT TIMESERIES root.ln.*.*.status -IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status -IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' -IoTDB > COUNT TIMESERIES root.** WHERE DATATYPE = INT64 -IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' -IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' -IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 -IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1 -IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 -IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 -``` - -### 3.6 Tag and Attribute Management - -```sql -create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) -``` - -* Rename the tag/attribute key - -```SQL -ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 -``` - -* Reset the tag/attribute value - -```SQL -ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 -``` - -* Delete the existing tag/attribute - -```SQL -ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 -``` - -* Add new tags - -```SQL -ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 -``` - -* Add new attributes - -```SQL -ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 -``` - -* Upsert alias, tags and attributes - -> add alias or a new key-value if the alias or key doesn't exist, otherwise, update the old one with new value. - -```SQL -ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4) -``` - -* Show timeseries using tags. Use TAGS(tagKey) to identify the tags used as filter key - -```SQL -SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -``` - -returns all the timeseries information that satisfy the where condition and match the pathPattern. SQL statements are as follows: - -```SQL -ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c -ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 -show timeseries root.ln.** where TAGS(unit)='c' -show timeseries root.ln.** where TAGS(description) contains 'test1' -``` - -- count timeseries using tags - -```SQL -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= -``` - -returns all the number of timeseries that satisfy the where condition and match the pathPattern. SQL statements are as follows: - -```SQL -count timeseries -count timeseries root.** where TAGS(unit)='c' -count timeseries root.** where TAGS(unit)='c' group by level = 2 -``` - -create aligned timeseries - -```SQL -create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) -``` - -The execution result is as follows: - -```SQL -IoTDB> show timeseries -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| -|root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -``` - -Support query: - -```SQL -IoTDB> show timeseries where TAGS(tag1)='v1' -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -``` - -The above operations are supported for timeseries tag, attribute updates, etc. - -## 4. NODE MANAGEMENT - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -### 4.1 Show Child Paths - -```SQL -SHOW CHILD PATHS pathPattern -``` - -### 4.2 Show Child Nodes - -```SQL -SHOW CHILD NODES pathPattern -``` - -### 4.3 Count Nodes - -```SQL -IoTDB > COUNT NODES root.** LEVEL=2 -IoTDB > COUNT NODES root.ln.** LEVEL=2 -IoTDB > COUNT NODES root.ln.wf01.** LEVEL=3 -IoTDB > COUNT NODES root.**.temperature LEVEL=3 -``` - -### 4.4 Show Devices - -```SQL -IoTDB> show devices -IoTDB> show devices root.ln.** -IoTDB> show devices root.ln.** where device contains 't' -IoTDB> show devices with database -IoTDB> show devices root.ln.** with database -``` - -### 4.5 Count Devices - -```SQL -IoTDB> show devices -IoTDB> count devices -IoTDB> count devices root.ln.** -``` - -## 5. INSERT & LOAD DATA - -### 5.1 Insert Data - -For more details, see document [Write-Data](../Basic-Concept/Write-Data). - -#### Use of INSERT Statements - -- Insert Single Timeseries - -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) -IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1') -``` - -- Insert Multiple Timeseries - -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (2, false, 'v2') -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') -``` - -- Use the Current System Timestamp as the Timestamp of the Data Point - -```SQL -IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') -``` - -#### Insert Data Into Aligned Timeseries - -```SQL -IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) -IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(1, 1, 1) -IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(2, 2, 2), (3, 3, 3) -IoTDB > select * from root.sg1.d1 -``` - -### 5.2 Load External TsFile Tool - -For more details, see document [Data Import](../Tools-System/Data-Import-Tool.md). - -#### Load with SQL - -1. Load a single tsfile by specifying a file path (absolute path). - -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` - - -2. Load a batch of files by specifying a folder path (absolute path). - -- `load '/Users/Desktop/data'` -- `load '/Users/Desktop/data' sglevel=1` -- `load '/Users/Desktop/data' onSuccess=delete` -- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` - -#### Load with Script - -``` -./load-rewrite.bat -f D:\IoTDB\data -h 192.168.0.101 -p 6667 -u root -pw root -``` - -## 6. DELETE DATA - -For more details, see document [Write-Delete-Data](../Basic-Concept/Write-Data). - -### 6.1 Delete Single Timeseries - -```sql -IoTDB > delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; -IoTDB > delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -IoTDB > delete from root.ln.wf02.wt02.status where time < 10 -IoTDB > delete from root.ln.wf02.wt02.status where time <= 10 -IoTDB > delete from root.ln.wf02.wt02.status where time < 20 and time > 10 -IoTDB > delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 -IoTDB > delete from root.ln.wf02.wt02.status where time > 20 -IoTDB > delete from root.ln.wf02.wt02.status where time >= 20 -IoTDB > delete from root.ln.wf02.wt02.status where time = 20 -IoTDB > delete from root.ln.wf02.wt02.status where time > 4 or time < 0 -Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic -expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' -IoTDB > delete from root.ln.wf02.wt02.status -``` - -### 6.2 Delete Multiple Timeseries - -```sql -IoTDB > delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00; -IoTDB > delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; -IoTDB> delete from root.ln.wf03.wt02.status where time < now() -Msg: The statement is executed successfully. -``` - -### 6.3 Delete Time Partition (experimental) - -```sql -IoTDB > DELETE PARTITION root.ln 0,1,2 -``` - -## 7. QUERY DATA - -For more details, see document [Query-Data](../Basic-Concept/Query-Data.md). - -```sql -SELECT [LAST] selectExpr [, selectExpr] ... - [INTO intoItem [, intoItem] ...] - FROM prefixPath [, prefixPath] ... - [WHERE whereCondition] - [GROUP BY { - ([startTime, endTime), interval [, slidingStep]) | - LEVEL = levelNum [, levelNum] ... | - TAGS(tagKey [, tagKey] ... ) | - VARIATION(expression[,delta][,ignoreNull=true/false]) | - CONDITION(expression,[keep>/>=/=/ select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 -``` - -#### Select Multiple Columns of Data Based on a Time Interval - -```sql -IoTDB > select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` - -#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals - -```sql -IoTDB > select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` - -#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals - -```sql -IoTDB > select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` - -#### Order By Time Query - -```sql -IoTDB > select * from root.ln.** where time > 1 order by time desc limit 10; -``` - -### 7.2 `SELECT` CLAUSE - -#### Use Alias - -```sql -IoTDB > select s1 as temperature, s2 as speed from root.ln.wf01.wt01; -``` - -#### Nested Expressions - -##### Nested Expressions with Time Series Query - -```sql -IoTDB > select a, - b, - ((a + 1) * 2 - 1) % 2 + 1.5, - sin(a + sin(a + sin(b))), - -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 -from root.sg1; - -IoTDB > select (a + b) * 2 + sin(a) from root.sg - -IoTDB > select (a + *) / 2 from root.sg1 - -IoTDB > select (a + b) * 3 from root.sg, root.ln -``` - -##### Nested Expressions query with aggregations - -```sql -IoTDB > select avg(temperature), - sin(avg(temperature)), - avg(temperature) + 1, - -sum(hardware), - avg(temperature) + sum(hardware) -from root.ln.wf01.wt01; - -IoTDB > select avg(*), - (avg(*) + 1) * 3 / 2 -1 -from root.sg1 - -IoTDB > select avg(temperature), - sin(avg(temperature)), - avg(temperature) + 1, - -sum(hardware), - avg(temperature) + sum(hardware) as custom_sum -from root.ln.wf01.wt01 -GROUP BY([10, 90), 10ms); -``` - -#### Last Query - -```sql -IoTDB > select last status from root.ln.wf01.wt01 -IoTDB > select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 -IoTDB > select last * from root.ln.wf01.wt01 order by timeseries desc; -IoTDB > select last * from root.ln.wf01.wt01 order by dataType desc; -``` - -### 7.3 `WHERE` CLAUSE - -#### Time Filter - -```sql -IoTDB > select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; -IoTDB > select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; -IoTDB > select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` - -#### Value Filter - -```sql -IoTDB > select temperature from root.sg1.d1 where temperature > 36.5; -IoTDB > select status from root.sg1.d1 where status = true; -IoTDB > select temperature from root.sg1.d1 where temperature between 36.5 and 40; -IoTDB > select temperature from root.sg1.d1 where temperature not between 36.5 and 40; -IoTDB > select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -IoTDB > select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -IoTDB > select code from root.sg1.d1 where temperature is null; -IoTDB > select code from root.sg1.d1 where temperature is not null; -``` - -#### Fuzzy Query - -- Fuzzy matching using `Like` - -```sql -IoTDB > select * from root.sg.d1 where value like '%cc%' -IoTDB > select * from root.sg.device where value like '_b_' -``` - -- Fuzzy matching using `Regexp` - -```sql -IoTDB > select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -IoTDB > select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -``` - -### 7.4 `GROUP BY` CLAUSE - -- Aggregate By Time without Specifying the Sliding Step Length - -```sql -IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); -``` - -- Aggregate By Time Specifying the Sliding Step Length - -```sql -IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); -``` - -- Aggregate by Natural Month - -```sql -IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -``` - -- Left Open And Right Close Range - -```sql -IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); -``` - -- Aggregation By Variation - -```sql -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6+s5, 10) -``` - -- Aggregation By Condition - -```sql -IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=true) -IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=false) -``` - -- Aggregation By Session - -```sql -IoTDB > select __endTime,count(*) from root.** group by session(1d) -IoTDB > select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device -``` - -- Aggregation By Count - -```sql -IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) -IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) -``` - -- Aggregation By Level - -```sql -IoTDB > select count(status) from root.** group by level = 1 -IoTDB > select count(status) from root.** group by level = 3 -IoTDB > select count(status) from root.** group by level = 1, 3 -IoTDB > select max_value(temperature) from root.** group by level = 0 -IoTDB > select count(*) from root.ln.** group by level = 2 -``` - -- Aggregate By Time with Level Clause - -```sql -IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; -IoTDB > select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; -``` - -- Aggregation query by one single tag - -```sql -IoTDB > SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); -``` - -- Aggregation query by multiple tags - -```sql -IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); -``` - -- Downsampling Aggregation by tags based on Time Window - -```sql -IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); -``` - -### 7.5 `HAVING` CLAUSE - -Correct: - -```sql -IoTDB > select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 1 -IoTDB > select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device -``` - -Incorrect: - -```sql -IoTDB > select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 -IoTDB > select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 -IoTDB > select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 -IoTDB > select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 -``` - -### 7.6 `FILL` CLAUSE - -#### `PREVIOUS` Fill - -```sql -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); -``` - -#### `PREVIOUS` FILL and specify the fill timeout threshold -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); -``` - -#### `LINEAR` Fill - -```sql -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); -``` - -#### Constant Fill - -```sql -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); -``` - -### 7.7 `LIMIT` and `SLIMIT` CLAUSES (PAGINATION) - -#### Row Control over Query Results - -```sql -IoTDB > select status, temperature from root.ln.wf01.wt01 limit 10 -IoTDB > select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 -IoTDB > select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 2 offset 3 -IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 5 offset 3 -``` - -#### Column Control over Query Results - -```sql -IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 -IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 -IoTDB > select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 -``` - -#### Row and Column Control over Query Results - -```sql -IoTDB > select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 -``` - -### 7.8 `ORDER BY` CLAUSE - -#### Order by in ALIGN BY TIME mode - -```sql -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; -``` - -#### Order by in ALIGN BY DEVICE mode - -```sql -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -IoTDB > select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device -``` - -#### Order by arbitrary expressions - -```sql -IoTDB > select score from root.** order by score desc align by device -IoTDB > select score,total from root.one order by base+score+bonus desc -IoTDB > select score,total from root.one order by total desc -IoTDB > select base, score, bonus, total from root.** order by total desc NULLS Last, - score desc NULLS Last, - bonus desc NULLS Last, - time desc align by device -IoTDB > select min_value(total) from root.** order by min_value(total) asc align by device -IoTDB > select min_value(total),max_value(base) from root.** order by max_value(total) desc align by device -IoTDB > select score from root.** order by device asc, score desc, time asc align by device -``` - -### 7.9 `ALIGN BY` CLAUSE - -#### Align by Device - -```sql -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -``` - -### 7.10 `INTO` CLAUSE (QUERY WRITE-BACK) - -```sql -IoTDB > select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; -IoTDB > select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); -IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -IoTDB > select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; -``` - -- Using variable placeholders: - -```sql -IoTDB > select s1, s2 -into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) -from root.sg.d1, root.sg.d2; - -IoTDB > select d1.s1, d1.s2, d2.s3, d3.s4 -into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) -from root.sg; - -IoTDB > select * into root.sg_bk.::(::) from root.sg.**; - -IoTDB > select s1, s2, s3, s4 -into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) -from root.sg.d1, root.sg.d2, root.sg.d3 -align by device; - -IoTDB > select avg(s1), sum(s2) + sum(s3), count(s4) -into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) -from root.** -align by device; - -IoTDB > select * into ::(backup_${4}) from root.sg.** align by device; - -IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -``` - -## 8. Maintennance -Generate the corresponding query plan: -``` -explain select s1,s2 from root.sg.d1 -``` -Execute the corresponding SQL, analyze the execution and output: -``` -explain analyze select s1,s2 from root.sg.d1 order by s1 -``` -## 9. OPERATOR - -For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). - -### 9.1 Arithmetic Operators - -For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-operators). - -```sql -select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 -``` - -### 9.2 Comparison Operators - -For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-operators). - -```sql -# Basic comparison operators -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; - -# `BETWEEN ... AND ...` operator -select temperature from root.sg1.d1 where temperature between 36.5 and 40; -select temperature from root.sg1.d1 where temperature not between 36.5 and 40; - -# Fuzzy matching operator: Use `Like` for fuzzy matching -select * from root.sg.d1 where value like '%cc%' -select * from root.sg.device where value like '_b_' - -# Fuzzy matching operator: Use `Regexp` for fuzzy matching -select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -select b, b like '1%', b regexp '[0-2]' from root.test; - -# `IS NULL` operator -select code from root.sg1.d1 where temperature is null; -select code from root.sg1.d1 where temperature is not null; - -# `IN` operator -select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -select a, a in (1, 2) from root.test; -``` - -### 9.3 Logical Operators - -For details and examples, see the document [Logical Operators](./Operator-and-Expression.md#logical-operators). - -```sql -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; -``` - -## 10. BUILT-IN FUNCTIONS - -For more details, see document [Operator-and-Expression](./Operator-and-Expression.md#built-in-functions). - -### 10.1 Aggregate Functions - -For details and examples, see the document [Aggregate Functions](./Operator-and-Expression.md#aggregate-functions). - -```sql -select count(status) from root.ln.wf01.wt01; - -select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; -select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; - -select time_duration(s1) from root.db.d1; -``` - -### 10.2 Arithmetic Functions - -For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-functions). - -```sql -select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; -select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; -``` - -### 10.3 Comparison Functions - -For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-functions). - -```sql -select ts, on_off(ts, 'threshold'='2') from root.test; -select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; -``` - -### 10.4 String Processing Functions - -For details and examples, see the document [String Processing](./Operator-and-Expression.md#string-processing-functions). - -```sql -select s1, string_contains(s1, 's'='warn') from root.sg1.d4; -select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; -select s1, length(s1) from root.sg1.d1 -select s1, locate(s1, "target"="1") from root.sg1.d1 -select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 -select s1, startswith(s1, "target"="1") from root.sg1.d1 -select s1, endswith(s1, "target"="1") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 -select s1, substring(s1 from 1 for 2) from root.sg1.d1 -select s1, replace(s1, 'es', 'tt') from root.sg1.d1 -select s1, upper(s1) from root.sg1.d1 -select s1, lower(s1) from root.sg1.d1 -select s3, trim(s3) from root.sg1.d1 -select s1, s2, strcmp(s1, s2) from root.sg1.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 -select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 -select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 -``` - -### 10.5 Data Type Conversion Function - -For details and examples, see the document [Data Type Conversion Function](./Operator-and-Expression.md#data-type-conversion-function). - -```sql -SELECT cast(s1 as INT32) from root.sg -``` - -### 10.6 Constant Timeseries Generating Functions - -For details and examples, see the document [Constant Timeseries Generating Functions](./Operator-and-Expression.md#constant-timeseries-generating-functions). - -```sql -select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; -``` - -### 10.7 Selector Functions - -For details and examples, see the document [Selector Functions](./Operator-and-Expression.md#selector-functions). - -```sql -select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; -``` - -### 10.8 Continuous Interval Functions - -For details and examples, see the document [Continuous Interval Functions](./Operator-and-Expression.md#continuous-interval-functions). - -```sql -select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; -``` - -### 10.9 Variation Trend Calculation Functions - -For details and examples, see the document [Variation Trend Calculation Functions](./Operator-and-Expression.md#variation-trend-calculation-functions). - -```sql -select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; - -SELECT DIFF(s1), DIFF(s2) from root.test; -SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; -``` - -### 10.10 Sample Functions - -For details and examples, see the document [Sample Functions](./Operator-and-Expression.md#sample-functions). - -```sql -select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; -select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; -select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; -select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; - -select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 -select M4(s1,'windowSize'='10') from root.vehicle.d1 -``` - -### 10.11 Change Points Function - -For details and examples, see the document [Time-Series](./Operator-and-Expression.md#change-points-function). - -```sql -select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 -``` - -## 11. DATA QUALITY FUNCTION LIBRARY - -For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). - -### 11.1 Data Quality - -For details and examples, see the document [Data-Quality](../SQL-Manual/UDF-Libraries.md#data-quality). - -```sql -# Completeness -select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Consistency -select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Timeliness -select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Validity -select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Accuracy -select Accuracy(t1,t2,t3,m1,m2,m3) from root.test -``` - -### 11.2 Data Profiling - -For details and examples, see the document [Data-Profiling](../SQL-Manual/UDF-Libraries.md#data-profiling). - -```sql -# ACF -select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 - -# Distinct -select distinct(s2) from root.test.d2 - -# Histogram -select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 - -# Integral -select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 -select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 - -# IntegralAvg -select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 - -# Mad -select mad(s0) from root.test -select mad(s0, "error"="0.01") from root.test - -# Median -select median(s0, "error"="0.01") from root.test - -# MinMax -select minmax(s1) from root.test - -# Mode -select mode(s2) from root.test.d2 - -# MvAvg -select mvavg(s1, "window"="3") from root.test - -# PACF -select pacf(s1, "lag"="5") from root.test - -# Percentile -select percentile(s0, "rank"="0.2", "error"="0.01") from root.test - -# Quantile -select quantile(s0, "rank"="0.2", "K"="800") from root.test - -# Period -select period(s1) from root.test.d3 - -# QLB -select QLB(s1) from root.test.d1 - -# Resample -select resample(s1,'every'='5m','interp'='linear') from root.test.d1 -select resample(s1,'every'='30m','aggr'='first') from root.test.d1 -select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 - -# Sample -select sample(s1,'method'='reservoir','k'='5') from root.test.d1 -select sample(s1,'method'='isometric','k'='5') from root.test.d1 - -# Segment -select segment(s1, "error"="0.1") from root.test - -# Skew -select skew(s1) from root.test.d1 - -# Spline -select spline(s1, "points"="151") from root.test - -# Spread -select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 - -# Stddev -select stddev(s1) from root.test.d1 - -# ZScore -select zscore(s1) from root.test -``` - -### 11.3 Anomaly Detection - -For details and examples, see the document [Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#anomaly-detection). - -```sql -# IQR -select iqr(s1) from root.test - -# KSigma -select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# LOF -select lof(s1,s2) from root.test.d1 where time<1000 -select lof(s1, "method"="series") from root.test.d1 where time<1000 - -# MissDetect -select missdetect(s2,'minlen'='10') from root.test.d2 - -# Range -select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# TwoSidedFilter -select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test - -# Outlier -select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test - -# MasterTrain -select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test - -# MasterDetect -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test -``` - -### 11.4 Frequency Domain - -For details and examples, see the document [Frequency-Domain](../SQL-Manual/UDF-Libraries.md#frequency-domain-analysis). - -```sql -# Conv -select conv(s1,s2) from root.test.d2 - -# Deconv -select deconv(s3,s2) from root.test.d2 -select deconv(s3,s2,'result'='remainder') from root.test.d2 - -# DWT -select dwt(s1,"method"="haar") from root.test.d1 - -# FFT -select fft(s1) from root.test.d1 -select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 - -# HighPass -select highpass(s1,'wpass'='0.45') from root.test.d1 - -# IFFT -select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 - -# LowPass -select lowpass(s1,'wpass'='0.45') from root.test.d1 - -# Envelope -select envelope(s1) from root.test.d1 -``` - -### 11.5 Data Matching - -For details and examples, see the document [Data-Matching](../SQL-Manual/UDF-Libraries.md#data-matching). - -```sql -# Cov -select cov(s1,s2) from root.test.d2 - -# DTW -select dtw(s1,s2) from root.test.d2 - -# Pearson -select pearson(s1,s2) from root.test.d2 - -# PtnSym -select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 - -# XCorr -select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 -``` - -### 11.6 Data Repairing - -For details and examples, see the document [Data-Repairing](../SQL-Manual/UDF-Libraries.md#data-repairing). - -```sql -# TimestampRepair -select timestamprepair(s1,'interval'='10000') from root.test.d2 -select timestamprepair(s1) from root.test.d2 - -# ValueFill -select valuefill(s1) from root.test.d2 -select valuefill(s1,"method"="previous") from root.test.d2 - -# ValueRepair -select valuerepair(s1) from root.test.d2 -select valuerepair(s1,'method'='LsGreedy') from root.test.d2 - -# MasterRepair -select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test - -# SeasonalRepair -select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 -select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 -``` - -### 11.7 Series Discovery - -For details and examples, see the document [Series-Discovery](../SQL-Manual/UDF-Libraries.md#series-discovery). - -```sql -# ConsecutiveSequences -select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 -select consecutivesequences(s1,s2) from root.test.d1 - -# ConsecutiveWindows -select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 -``` - -### 11.8 Machine Learning - -For details and examples, see the document [Machine-Learning](../SQL-Manual/UDF-Libraries.md#machine-learning). - -```sql -# AR -select ar(s0,"p"="2") from root.test.d0 - -# Representation -select representation(s0,"tb"="3","vb"="2") from root.test.d0 - -# RM -select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 -``` - -## 12. LAMBDA EXPRESSION - -For details and examples, see the document [Lambda](../SQL-Manual/UDF-Libraries.md#lambda-expression). - -```sql -select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` -``` - -## 13. CONDITIONAL EXPRESSION - -For details and examples, see the document [Conditional Expressions](../SQL-Manual/UDF-Libraries.md#conditional-expressions). - -```sql -select T, P, case -when 1000=1050 then "bad temperature" -when P<=1000000 or P>=1100000 then "bad pressure" -end as `result` -from root.test1 - -select str, case -when str like "%cc%" then "has cc" -when str like "%dd%" then "has dd" -else "no cc and dd" end as `result` -from root.test2 - -select -count(case when x<=1 then 1 end) as `(-∞,1]`, -count(case when 1 -[RESAMPLE - [EVERY ] - [BOUNDARY ] - [RANGE [, end_time_offset]] -] -[TIMEOUT POLICY BLOCKED|DISCARD] -BEGIN - SELECT CLAUSE - INTO CLAUSE - FROM CLAUSE - [WHERE CLAUSE] - [GROUP BY([, ]) [, level = ]] - [HAVING CLAUSE] - [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] - [LIMIT rowLimit OFFSET rowOffset] - [ALIGN BY DEVICE] -END -``` - -### 15.1 Configuring execution intervals - -```sql -CREATE CONTINUOUS QUERY cq1 -RESAMPLE EVERY 20s -BEGIN -SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) -END -``` - -### 15.2 Configuring time range for resampling - -```sql -CREATE CONTINUOUS QUERY cq2 -RESAMPLE RANGE 40s -BEGIN - SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) -END -``` - -### 15.3 Configuring execution intervals and CQ time ranges - -```sql -CREATE CONTINUOUS QUERY cq3 -RESAMPLE EVERY 20s RANGE 40s -BEGIN - SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) - FILL(100.0) -END -``` - -### 15.4 Configuring end_time_offset for CQ time range - -```sql -CREATE CONTINUOUS QUERY cq4 -RESAMPLE EVERY 20s RANGE 40s, 20s -BEGIN - SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) - FILL(100.0) -END -``` - -### 15.5 CQ without group by clause - -```sql -CREATE CONTINUOUS QUERY cq5 -RESAMPLE EVERY 20s -BEGIN - SELECT temperature + 1 - INTO root.precalculated_sg.::(temperature) - FROM root.ln.*.* - align by device -END -``` - -### 15.6 CQ Management - -#### Listing continuous queries - -```sql -SHOW (CONTINUOUS QUERIES | CQS) -``` - -#### Dropping continuous queries - -```sql -DROP (CONTINUOUS QUERY | CQ) -``` - -#### Altering continuous queries - -CQs can't be altered once they're created. To change a CQ, you must `DROP` and re`CREATE` it with the updated settings. - -## 16. USER-DEFINED FUNCTION (UDF) - -For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). - -### 16.1 UDF Registration - -```sql -CREATE FUNCTION AS (USING URI URI-STRING)? -``` - -### 16.2 UDF Deregistration - -```sql -DROP FUNCTION -``` - -### 16.3 UDF Queries - -```sql -SELECT example(*) from root.sg.d1 -SELECT example(s1, *) from root.sg.d1 -SELECT example(*, *) from root.sg.d1 - -SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; -SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; - -SELECT s1, s2, example(s1, s2) FROM root.sg.d1; -SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; -SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; -SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; -``` - -### 16.4 Show All Registered UDFs - -```sql -SHOW FUNCTIONS -``` - -## 17. ADMINISTRATION MANAGEMENT - -For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). - -### 17.1 SQL Statements - -- Create user (Requires MANAGE_USER permission) - -```SQL -CREATE USER -eg: CREATE USER user1 'passwd' -``` - -- Delete user (Requires MANAGE_USER permission) - -```sql -DROP USER -eg: DROP USER user1 -``` - -- Create role (Requires MANAGE_ROLE permission) - -```sql -CREATE ROLE -eg: CREATE ROLE role1 -``` - -- Delete role (Requires MANAGE_ROLE permission) - -```sql -DROP ROLE -eg: DROP ROLE role1 -``` - -- Grant role to user (Requires MANAGE_ROLE permission) - -```sql -GRANT ROLE TO -eg: GRANT ROLE admin TO user1 -``` - -- Revoke role from user(Requires MANAGE_ROLE permission) - -```sql -REVOKE ROLE FROM -eg: REVOKE ROLE admin FROM user1 -``` - -- List all user (Requires MANAGE_USER permission) - -```sql -LIST USER -``` - -- List all role (Requires MANAGE_ROLE permission) - -```sql -LIST ROLE -``` - -- List all users granted specific role.(Requires MANAGE_USER permission) - -```sql -LIST USER OF ROLE -eg: LIST USER OF ROLE roleuser -``` - -- List all role granted to specific user. - -```sql -LIST ROLE OF USER -eg: LIST ROLE OF USER tempuser -``` - -- List all privileges of user - -```sql -LIST PRIVILEGES OF USER ; -eg: LIST PRIVILEGES OF USER tempuser; -``` - -- List all privileges of role - -```sql -LIST PRIVILEGES OF ROLE ; -eg: LIST PRIVILEGES OF ROLE actor; -``` - -- Update password - -```sql -ALTER USER SET PASSWORD ; -eg: ALTER USER tempuser SET PASSWORD 'newpwd'; -``` - -### 17.2 Authorization and Deauthorization - - -```sql -GRANT ON TO ROLE/USER [WITH GRANT OPTION]; -eg: GRANT READ ON root.** TO ROLE role1; -eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; -eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; -eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; -eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; -``` - -```sql -REVOKE ON FROM ROLE/USER ; -eg: REVOKE READ ON root.** FROM ROLE role1; -eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; -eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; -eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; -eg: REVOKE ALL ON ROOT.** FROM USER user1; -``` - - -#### Delete Time Partition (experimental) - -``` -Eg: IoTDB > DELETE PARTITION root.ln 0,1,2 -``` - -#### Continuous Query,CQ - -``` -Eg: IoTDB > CREATE CONTINUOUS QUERY cq1 BEGIN SELECT max_value(temperature) INTO temperature_max FROM root.ln.*.* GROUP BY time(10s) END -``` - -#### Maintenance Command - -- FLUSH - -``` -Eg: IoTDB > flush -``` - -- MERGE - -``` -Eg: IoTDB > MERGE -Eg: IoTDB > FULL MERGE -``` - -- CLEAR CACHE - -```sql -Eg: IoTDB > CLEAR CACHE -``` - -- START REPAIR DATA - -```sql -Eg: IoTDB > START REPAIR DATA -``` - -- STOP REPAIR DATA - -```sql -Eg: IoTDB > STOP REPAIR DATA -``` - -- SET SYSTEM TO READONLY / WRITABLE - -``` -Eg: IoTDB > SET SYSTEM TO READONLY / WRITABLE -``` - -- Query abort - -``` -Eg: IoTDB > KILL QUERY 1 -``` \ No newline at end of file +--> \ No newline at end of file diff --git a/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_apache.md b/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_apache.md new file mode 100644 index 000000000..18ebb4f43 --- /dev/null +++ b/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_apache.md @@ -0,0 +1,1759 @@ + + +# SQL Manual + +## 1. DATABASE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 1.1 Create Database + +```sql +IoTDB > create database root.ln +IoTDB > create database root.sgcc +``` + +### 1.2 Show Databases + +```sql +IoTDB> SHOW DATABASES +IoTDB> SHOW DATABASES root.** +``` + +### 1.3 Delete Database + +```sql +IoTDB > DELETE DATABASE root.ln +IoTDB > DELETE DATABASE root.sgcc +// delete all data, all timeseries and all databases +IoTDB > DELETE DATABASE root.** +``` + +### 1.4 Count Databases + +```sql +IoTDB> count databases +IoTDB> count databases root.* +IoTDB> count databases root.sgcc.* +IoTDB> count databases root.sgcc +``` + +### 1.5 Setting up heterogeneous databases (Advanced operations) + +#### Set heterogeneous parameters when creating a Database + +```sql +CREATE DATABASE root.db WITH SCHEMA_REPLICATION_FACTOR=1, DATA_REPLICATION_FACTOR=3, SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Adjust heterogeneous parameters at run time + +```sql +ALTER DATABASE root.db WITH SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Show heterogeneous databases + +```sql +SHOW DATABASES DETAILS +``` + +### 1.6 TTL + +#### Set TTL + +```sql +IoTDB> set ttl to root.ln 3600000 +IoTDB> set ttl to root.sgcc.** 3600000 +IoTDB> set ttl to root.** 3600000 +``` + +#### Unset TTL + +```sql +IoTDB> unset ttl from root.ln +IoTDB> unset ttl from root.sgcc.** +IoTDB> unset ttl from root.** +``` + +#### Show TTL + +```sql +IoTDB> SHOW ALL TTL +IoTDB> SHOW TTL ON StorageGroupNames +IoTDB> SHOW DEVICES +``` + +## 2. DEVICE TEMPLATE + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +![img](/img/%E6%A8%A1%E6%9D%BF.png) + + + + + +![img](/img/templateEN.jpg) + +### 2.1 Create Device Template + +**Example 1:** Create a template containing two non-aligned timeseires + +```shell +IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) +``` + +**Example 2:** Create a template containing a group of aligned timeseires + +```shell +IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) +``` + +The` lat` and `lon` measurements are aligned. + +### 2.2 Set Device Template + +```sql +IoTDB> set device template t1 to root.sg1.d1 +``` + +### 2.3 Activate Device Template + +```sql +IoTDB> set device template t1 to root.sg1.d1 +IoTDB> set device template t2 to root.sg1.d2 +IoTDB> create timeseries using device template on root.sg1.d1 +IoTDB> create timeseries using device template on root.sg1.d2 +``` + +### 2.4 Show Device Template + +```sql +IoTDB> show device templates +IoTDB> show nodes in device template t1 +IoTDB> show paths set device template t1 +IoTDB> show paths using device template t1 +``` + +### 2.5 Deactivate Device Template + +```sql +IoTDB> delete timeseries of device template t1 from root.sg1.d1 +IoTDB> deactivate device template t1 from root.sg1.d1 +IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* +IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* +``` + +### 2.6 Unset Device Template + +```sql +IoTDB> unset device template t1 from root.sg1.d1 +``` + +### 2.7 Drop Device Template + +```sql +IoTDB> drop device template t1 +``` + +### 2.8 Alter Device Template + +```sql +IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) +``` + +## 3. TIMESERIES MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 3.1 Create Timeseries + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- From v0.13, you can use a simplified version of the SQL statements to create timeseries: + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- Notice that when in the CREATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below: + +```sql +IoTDB > create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +error: encoding TS_2DIFF does not support BOOLEAN +``` + +### 3.2 Create Aligned Timeseries + +```sql +IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT , longitude FLOAT) +``` + +### 3.3 Delete Timeseries + +```sql +IoTDB> delete timeseries root.ln.wf01.wt01.status +IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +IoTDB> delete timeseries root.ln.wf02.* +IoTDB> drop timeseries root.ln.wf02.* +``` + +### 3.4 Show Timeseries + +```sql +IoTDB> show timeseries root.** +IoTDB> show timeseries root.ln.** +IoTDB> show timeseries root.ln.** limit 10 offset 10 +IoTDB> show timeseries root.ln.** where timeseries contains 'wf01.wt' +IoTDB> show timeseries root.ln.** where dataType=FLOAT +``` + +### 3.5 Count Timeseries + +```sql +IoTDB > COUNT TIMESERIES root.** +IoTDB > COUNT TIMESERIES root.ln.** +IoTDB > COUNT TIMESERIES root.ln.*.*.status +IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +IoTDB > COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1 +IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +### 3.6 Tag and Attribute Management + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +* Rename the tag/attribute key + +```SQL +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +* Reset the tag/attribute value + +```SQL +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +* Delete the existing tag/attribute + +```SQL +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +* Add new tags + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +* Add new attributes + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +* Upsert alias, tags and attributes + +> add alias or a new key-value if the alias or key doesn't exist, otherwise, update the old one with new value. + +```SQL +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +* Show timeseries using tags. Use TAGS(tagKey) to identify the tags used as filter key + +```SQL +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +returns all the timeseries information that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- count timeseries using tags + +```SQL +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +returns all the number of timeseries that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +create aligned timeseries + +```SQL +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +The execution result is as follows: + +```SQL +IoTDB> show timeseries ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| +|root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +Support query: + +```SQL +IoTDB> show timeseries where TAGS(tag1)='v1' ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +The above operations are supported for timeseries tag, attribute updates, etc. + +## 4. NODE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 4.1 Show Child Paths + +```SQL +SHOW CHILD PATHS pathPattern +``` + +### 4.2 Show Child Nodes + +```SQL +SHOW CHILD NODES pathPattern +``` + +### 4.3 Count Nodes + +```SQL +IoTDB > COUNT NODES root.** LEVEL=2 +IoTDB > COUNT NODES root.ln.** LEVEL=2 +IoTDB > COUNT NODES root.ln.wf01.** LEVEL=3 +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` + +### 4.4 Show Devices + +```SQL +IoTDB> show devices +IoTDB> show devices root.ln.** +IoTDB> show devices root.ln.** where device contains 't' +IoTDB> show devices with database +IoTDB> show devices root.ln.** with database +``` + +### 4.5 Count Devices + +```SQL +IoTDB> show devices +IoTDB> count devices +IoTDB> count devices root.ln.** +``` + +## 5. INSERT & LOAD DATA + +### 5.1 Insert Data + +For more details, see document [Write-Data](../Basic-Concept/Write-Data). + +#### Use of INSERT Statements + +- Insert Single Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1') +``` + +- Insert Multiple Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (2, false, 'v2') +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` + +- Use the Current System Timestamp as the Timestamp of the Data Point + +```SQL +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` + +#### Insert Data Into Aligned Timeseries + +```SQL +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(1, 1, 1) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +IoTDB > select * from root.sg1.d1 +``` + +### 5.2 Load External TsFile Tool + +For more details, see document [Data Import](../Tools-System/Data-Import-Tool.md). + +#### Load with SQL + +1. Load a single tsfile by specifying a file path (absolute path). + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +2. Load a batch of files by specifying a folder path (absolute path). + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +#### Load with Script + +``` +./load-rewrite.bat -f D:\IoTDB\data -h 192.168.0.101 -p 6667 -u root -pw root +``` + +## 6. DELETE DATA + +For more details, see document [Write-Delete-Data](../Basic-Concept/Write-Data). + +### 6.1 Delete Single Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time < 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time > 20 +IoTDB > delete from root.ln.wf02.wt02.status where time >= 20 +IoTDB > delete from root.ln.wf02.wt02.status where time = 20 +IoTDB > delete from root.ln.wf02.wt02.status where time > 4 or time < 0 +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +IoTDB > delete from root.ln.wf02.wt02.status +``` + +### 6.2 Delete Multiple Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +IoTDB> delete from root.ln.wf03.wt02.status where time < now() +Msg: The statement is executed successfully. +``` + +### 6.3 Delete Time Partition (experimental) + +```sql +IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +## 7. QUERY DATA + +For more details, see document [Query-Data](../Basic-Concept/Query-Data.md). + +```sql +SELECT [LAST] selectExpr [, selectExpr] ... + [INTO intoItem [, intoItem] ...] + FROM prefixPath [, prefixPath] ... + [WHERE whereCondition] + [GROUP BY { + ([startTime, endTime), interval [, slidingStep]) | + LEVEL = levelNum [, levelNum] ... | + TAGS(tagKey [, tagKey] ... ) | + VARIATION(expression[,delta][,ignoreNull=true/false]) | + CONDITION(expression,[keep>/>=/=/ select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` + +#### Select Multiple Columns of Data Based on a Time Interval + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals + +```sql +IoTDB > select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals + +```sql +IoTDB > select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Order By Time Query + +```sql +IoTDB > select * from root.ln.** where time > 1 order by time desc limit 10; +``` + +### 7.2 `SELECT` CLAUSE + +#### Use Alias + +```sql +IoTDB > select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` + +#### Nested Expressions + +##### Nested Expressions with Time Series Query + +```sql +IoTDB > select a, + b, + ((a + 1) * 2 - 1) % 2 + 1.5, + sin(a + sin(a + sin(b))), + -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 +from root.sg1; + +IoTDB > select (a + b) * 2 + sin(a) from root.sg + +IoTDB > select (a + *) / 2 from root.sg1 + +IoTDB > select (a + b) * 3 from root.sg, root.ln +``` + +##### Nested Expressions query with aggregations + +```sql +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) +from root.ln.wf01.wt01; + +IoTDB > select avg(*), + (avg(*) + 1) * 3 / 2 -1 +from root.sg1 + +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) as custom_sum +from root.ln.wf01.wt01 +GROUP BY([10, 90), 10ms); +``` + +#### Last Query + +```sql +IoTDB > select last status from root.ln.wf01.wt01 +IoTDB > select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +IoTDB > select last * from root.ln.wf01.wt01 order by timeseries desc; +IoTDB > select last * from root.ln.wf01.wt01 order by dataType desc; +``` + +### 7.3 `WHERE` CLAUSE + +#### Time Filter + +```sql +IoTDB > select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Value Filter + +```sql +IoTDB > select temperature from root.sg1.d1 where temperature > 36.5; +IoTDB > select status from root.sg1.d1 where status = true; +IoTDB > select temperature from root.sg1.d1 where temperature between 36.5 and 40; +IoTDB > select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +IoTDB > select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where temperature is null; +IoTDB > select code from root.sg1.d1 where temperature is not null; +``` + +#### Fuzzy Query + +- Fuzzy matching using `Like` + +```sql +IoTDB > select * from root.sg.d1 where value like '%cc%' +IoTDB > select * from root.sg.device where value like '_b_' +``` + +- Fuzzy matching using `Regexp` + +```sql +IoTDB > select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +IoTDB > select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 7.4 `GROUP BY` CLAUSE + +- Aggregate By Time without Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` + +- Aggregate By Time Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` + +- Aggregate by Natural Month + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` + +- Left Open And Right Close Range + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` + +- Aggregation By Variation + +```sql +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6+s5, 10) +``` + +- Aggregation By Condition + +```sql +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=true) +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=false) +``` + +- Aggregation By Session + +```sql +IoTDB > select __endTime,count(*) from root.** group by session(1d) +IoTDB > select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` + +- Aggregation By Count + +```sql +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` + +- Aggregation By Level + +```sql +IoTDB > select count(status) from root.** group by level = 1 +IoTDB > select count(status) from root.** group by level = 3 +IoTDB > select count(status) from root.** group by level = 1, 3 +IoTDB > select max_value(temperature) from root.** group by level = 0 +IoTDB > select count(*) from root.ln.** group by level = 2 +``` + +- Aggregate By Time with Level Clause + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +IoTDB > select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` + +- Aggregation query by one single tag + +```sql +IoTDB > SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` + +- Aggregation query by multiple tags + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` + +- Downsampling Aggregation by tags based on Time Window + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` + +### 7.5 `HAVING` CLAUSE + +Correct: + +```sql +IoTDB > select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 1 +IoTDB > select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device +``` + +Incorrect: + +```sql +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 +IoTDB > select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` + +### 7.6 `FILL` CLAUSE + +#### `PREVIOUS` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` + +#### `PREVIOUS` FILL and specify the fill timeout threshold +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` + +#### `LINEAR` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` + +#### Constant Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` + +### 7.7 `LIMIT` and `SLIMIT` CLAUSES (PAGINATION) + +#### Row Control over Query Results + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 10 +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +IoTDB > select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 2 offset 3 +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 5 offset 3 +``` + +#### Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +IoTDB > select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` + +#### Row and Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` + +### 7.8 `ORDER BY` CLAUSE + +#### Order by in ALIGN BY TIME mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` + +#### Order by in ALIGN BY DEVICE mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +IoTDB > select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` + +#### Order by arbitrary expressions + +```sql +IoTDB > select score from root.** order by score desc align by device +IoTDB > select score,total from root.one order by base+score+bonus desc +IoTDB > select score,total from root.one order by total desc +IoTDB > select base, score, bonus, total from root.** order by total desc NULLS Last, + score desc NULLS Last, + bonus desc NULLS Last, + time desc align by device +IoTDB > select min_value(total) from root.** order by min_value(total) asc align by device +IoTDB > select min_value(total),max_value(base) from root.** order by max_value(total) desc align by device +IoTDB > select score from root.** order by device asc, score desc, time asc align by device +``` + +### 7.9 `ALIGN BY` CLAUSE + +#### Align by Device + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` + +### 7.10 `INTO` CLAUSE (QUERY WRITE-BACK) + +```sql +IoTDB > select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +IoTDB > select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +IoTDB > select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` + +- Using variable placeholders: + +```sql +IoTDB > select s1, s2 +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) +from root.sg.d1, root.sg.d2; + +IoTDB > select d1.s1, d1.s2, d2.s3, d3.s4 +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) +from root.sg; + +IoTDB > select * into root.sg_bk.::(::) from root.sg.**; + +IoTDB > select s1, s2, s3, s4 +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) +from root.sg.d1, root.sg.d2, root.sg.d3 +align by device; + +IoTDB > select avg(s1), sum(s2) + sum(s3), count(s4) +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) +from root.** +align by device; + +IoTDB > select * into ::(backup_${4}) from root.sg.** align by device; + +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` + +## 8. Maintennance +Generate the corresponding query plan: +``` +explain select s1,s2 from root.sg.d1 +``` +Execute the corresponding SQL, analyze the execution and output: +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 9. OPERATOR + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 9.1 Arithmetic Operators + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-operators). + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 9.2 Comparison Operators + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-operators). + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 9.3 Logical Operators + +For details and examples, see the document [Logical Operators](./Operator-and-Expression.md#logical-operators). + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 10. BUILT-IN FUNCTIONS + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md#built-in-functions). + +### 10.1 Aggregate Functions + +For details and examples, see the document [Aggregate Functions](./Operator-and-Expression.md#aggregate-functions). + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 10.2 Arithmetic Functions + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-functions). + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 10.3 Comparison Functions + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-functions). + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 10.4 String Processing Functions + +For details and examples, see the document [String Processing](./Operator-and-Expression.md#string-processing-functions). + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 10.5 Data Type Conversion Function + +For details and examples, see the document [Data Type Conversion Function](./Operator-and-Expression.md#data-type-conversion-function). + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 10.6 Constant Timeseries Generating Functions + +For details and examples, see the document [Constant Timeseries Generating Functions](./Operator-and-Expression.md#constant-timeseries-generating-functions). + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 10.7 Selector Functions + +For details and examples, see the document [Selector Functions](./Operator-and-Expression.md#selector-functions). + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 10.8 Continuous Interval Functions + +For details and examples, see the document [Continuous Interval Functions](./Operator-and-Expression.md#continuous-interval-functions). + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 10.9 Variation Trend Calculation Functions + +For details and examples, see the document [Variation Trend Calculation Functions](./Operator-and-Expression.md#variation-trend-calculation-functions). + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 10.10 Sample Functions + +For details and examples, see the document [Sample Functions](./Operator-and-Expression.md#sample-functions). + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 10.11 Change Points Function + +For details and examples, see the document [Time-Series](./Operator-and-Expression.md#change-points-function). + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 11. DATA QUALITY FUNCTION LIBRARY + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 11.1 Data Quality + +For details and examples, see the document [Data-Quality](../SQL-Manual/UDF-Libraries.md#data-quality). + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 11.2 Data Profiling + +For details and examples, see the document [Data-Profiling](../SQL-Manual/UDF-Libraries.md#data-profiling). + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 11.3 Anomaly Detection + +For details and examples, see the document [Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#anomaly-detection). + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 11.4 Frequency Domain + +For details and examples, see the document [Frequency-Domain](../SQL-Manual/UDF-Libraries.md#frequency-domain-analysis). + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 11.5 Data Matching + +For details and examples, see the document [Data-Matching](../SQL-Manual/UDF-Libraries.md#data-matching). + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 11.6 Data Repairing + +For details and examples, see the document [Data-Repairing](../SQL-Manual/UDF-Libraries.md#data-repairing). + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 11.7 Series Discovery + +For details and examples, see the document [Series-Discovery](../SQL-Manual/UDF-Libraries.md#series-discovery). + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 11.8 Machine Learning + +For details and examples, see the document [Machine-Learning](../SQL-Manual/UDF-Libraries.md#machine-learning). + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 12. LAMBDA EXPRESSION + +For details and examples, see the document [Lambda](../SQL-Manual/UDF-Libraries.md#lambda-expression). + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 13. CONDITIONAL EXPRESSION + +For details and examples, see the document [Conditional Expressions](../SQL-Manual/UDF-Libraries.md#conditional-expressions). + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +### 15.1 Configuring execution intervals + +```sql +CREATE CONTINUOUS QUERY cq1 +RESAMPLE EVERY 20s +BEGIN +SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 15.2 Configuring time range for resampling + +```sql +CREATE CONTINUOUS QUERY cq2 +RESAMPLE RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 15.3 Configuring execution intervals and CQ time ranges + +```sql +CREATE CONTINUOUS QUERY cq3 +RESAMPLE EVERY 20s RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 15.4 Configuring end_time_offset for CQ time range + +```sql +CREATE CONTINUOUS QUERY cq4 +RESAMPLE EVERY 20s RANGE 40s, 20s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 15.5 CQ without group by clause + +```sql +CREATE CONTINUOUS QUERY cq5 +RESAMPLE EVERY 20s +BEGIN + SELECT temperature + 1 + INTO root.precalculated_sg.::(temperature) + FROM root.ln.*.* + align by device +END +``` + +### 15.6 CQ Management + +#### Listing continuous queries + +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` + +#### Dropping continuous queries + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` + +#### Altering continuous queries + +CQs can't be altered once they're created. To change a CQ, you must `DROP` and re`CREATE` it with the updated settings. + +## 16. USER-DEFINED FUNCTION (UDF) + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 16.1 UDF Registration + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +### 16.2 UDF Deregistration + +```sql +DROP FUNCTION +``` + +### 16.3 UDF Queries + +```sql +SELECT example(*) from root.sg.d1 +SELECT example(s1, *) from root.sg.d1 +SELECT example(*, *) from root.sg.d1 + +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; + +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` + +### 16.4 Show All Registered UDFs + +```sql +SHOW FUNCTIONS +``` + +## 17. ADMINISTRATION MANAGEMENT + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 17.1 SQL Statements + +- Create user (Requires MANAGE_USER permission) + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- Delete user (Requires MANAGE_USER permission) + +```sql +DROP USER +eg: DROP USER user1 +``` + +- Create role (Requires MANAGE_ROLE permission) + +```sql +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- Delete role (Requires MANAGE_ROLE permission) + +```sql +DROP ROLE +eg: DROP ROLE role1 +``` + +- Grant role to user (Requires MANAGE_ROLE permission) + +```sql +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- Revoke role from user(Requires MANAGE_ROLE permission) + +```sql +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- List all user (Requires MANAGE_USER permission) + +```sql +LIST USER +``` + +- List all role (Requires MANAGE_ROLE permission) + +```sql +LIST ROLE +``` + +- List all users granted specific role.(Requires MANAGE_USER permission) + +```sql +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- List all role granted to specific user. + +```sql +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- List all privileges of user + +```sql +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; +``` + +- List all privileges of role + +```sql +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- Update password + +```sql +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 17.2 Authorization and Deauthorization + + +```sql +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +```sql +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + + +#### Delete Time Partition (experimental) + +``` +Eg: IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +#### Continuous Query,CQ + +``` +Eg: IoTDB > CREATE CONTINUOUS QUERY cq1 BEGIN SELECT max_value(temperature) INTO temperature_max FROM root.ln.*.* GROUP BY time(10s) END +``` + +#### Maintenance Command + +- FLUSH + +``` +Eg: IoTDB > flush +``` + +- MERGE + +``` +Eg: IoTDB > MERGE +Eg: IoTDB > FULL MERGE +``` + +- CLEAR CACHE + +```sql +Eg: IoTDB > CLEAR CACHE +``` + +- START REPAIR DATA + +```sql +Eg: IoTDB > START REPAIR DATA +``` + +- STOP REPAIR DATA + +```sql +Eg: IoTDB > STOP REPAIR DATA +``` + +- SET SYSTEM TO READONLY / WRITABLE + +``` +Eg: IoTDB > SET SYSTEM TO READONLY / WRITABLE +``` + +- Query abort + +``` +Eg: IoTDB > KILL QUERY 1 +``` \ No newline at end of file diff --git a/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_timecho.md b/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_timecho.md new file mode 100644 index 000000000..436462b36 --- /dev/null +++ b/src/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_timecho.md @@ -0,0 +1,1681 @@ + + +# SQL Manual + +## 1. DATABASE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 1.1 Create Database + +```sql +IoTDB > create database root.ln +IoTDB > create database root.sgcc +``` + +### 1.2 Show Databases + +```sql +IoTDB> SHOW DATABASES +IoTDB> SHOW DATABASES root.** +``` + +### 1.3 Delete Database + +```sql +IoTDB > DELETE DATABASE root.ln +IoTDB > DELETE DATABASE root.sgcc +// delete all data, all timeseries and all databases +IoTDB > DELETE DATABASE root.** +``` + +### 1.4 Count Databases + +```sql +IoTDB> count databases +IoTDB> count databases root.* +IoTDB> count databases root.sgcc.* +IoTDB> count databases root.sgcc +``` + +### 1.5 Setting up heterogeneous databases (Advanced operations) + +#### Set heterogeneous parameters when creating a Database + +```sql +CREATE DATABASE root.db WITH SCHEMA_REPLICATION_FACTOR=1, DATA_REPLICATION_FACTOR=3, SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Adjust heterogeneous parameters at run time + +```sql +ALTER DATABASE root.db WITH SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Show heterogeneous databases + +```sql +SHOW DATABASES DETAILS +``` + +### 1.6 TTL + +#### Set TTL + +```sql +IoTDB> set ttl to root.ln 3600000 +IoTDB> set ttl to root.sgcc.** 3600000 +IoTDB> set ttl to root.** 3600000 +``` + +#### Unset TTL + +```sql +IoTDB> unset ttl from root.ln +IoTDB> unset ttl from root.sgcc.** +IoTDB> unset ttl from root.** +``` + +#### Show TTL + +```sql +IoTDB> SHOW ALL TTL +IoTDB> SHOW TTL ON StorageGroupNames +IoTDB> SHOW DEVICES +``` + + +## 2. TIMESERIES MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 2.1 Create Timeseries + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- From v0.13, you can use a simplified version of the SQL statements to create timeseries: + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- Notice that when in the CREATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below: + +```sql +IoTDB > create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +error: encoding TS_2DIFF does not support BOOLEAN +``` + +### 2.2 Create Aligned Timeseries + +```sql +IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT , longitude FLOAT) +``` + +### 2.3 Delete Timeseries + +```sql +IoTDB> delete timeseries root.ln.wf01.wt01.status +IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +IoTDB> delete timeseries root.ln.wf02.* +IoTDB> drop timeseries root.ln.wf02.* +``` + +### 2.4 Show Timeseries + +```sql +IoTDB> show timeseries root.** +IoTDB> show timeseries root.ln.** +IoTDB> show timeseries root.ln.** limit 10 offset 10 +IoTDB> show timeseries root.ln.** where timeseries contains 'wf01.wt' +IoTDB> show timeseries root.ln.** where dataType=FLOAT +``` + +### 2.5 Count Timeseries + +```sql +IoTDB > COUNT TIMESERIES root.** +IoTDB > COUNT TIMESERIES root.ln.** +IoTDB > COUNT TIMESERIES root.ln.*.*.status +IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +IoTDB > COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1 +IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +### 2.6 Tag and Attribute Management + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +* Rename the tag/attribute key + +```SQL +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +* Reset the tag/attribute value + +```SQL +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +* Delete the existing tag/attribute + +```SQL +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +* Add new tags + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +* Add new attributes + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +* Upsert alias, tags and attributes + +> add alias or a new key-value if the alias or key doesn't exist, otherwise, update the old one with new value. + +```SQL +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +* Show timeseries using tags. Use TAGS(tagKey) to identify the tags used as filter key + +```SQL +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +returns all the timeseries information that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- count timeseries using tags + +```SQL +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +returns all the number of timeseries that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +create aligned timeseries + +```SQL +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +The execution result is as follows: + +```SQL +IoTDB> show timeseries ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| +|root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +Support query: + +```SQL +IoTDB> show timeseries where TAGS(tag1)='v1' ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +The above operations are supported for timeseries tag, attribute updates, etc. + +## 3. NODE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 3.1 Show Child Paths + +```SQL +SHOW CHILD PATHS pathPattern +``` + +### 3.2 Show Child Nodes + +```SQL +SHOW CHILD NODES pathPattern +``` + +### 3.3 Count Nodes + +```SQL +IoTDB > COUNT NODES root.** LEVEL=2 +IoTDB > COUNT NODES root.ln.** LEVEL=2 +IoTDB > COUNT NODES root.ln.wf01.** LEVEL=3 +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` + +### 3.4 Show Devices + +```SQL +IoTDB> show devices +IoTDB> show devices root.ln.** +IoTDB> show devices root.ln.** where device contains 't' +IoTDB> show devices with database +IoTDB> show devices root.ln.** with database +``` + +### 3.5 Count Devices + +```SQL +IoTDB> show devices +IoTDB> count devices +IoTDB> count devices root.ln.** +``` + +## 4. INSERT & LOAD DATA + +### 4.1 Insert Data + +For more details, see document [Write-Data](../Basic-Concept/Write-Data). + +#### Use of INSERT Statements + +- Insert Single Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1') +``` + +- Insert Multiple Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (2, false, 'v2') +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` + +- Use the Current System Timestamp as the Timestamp of the Data Point + +```SQL +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` + +#### Insert Data Into Aligned Timeseries + +```SQL +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(1, 1, 1) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +IoTDB > select * from root.sg1.d1 +``` + +### 4.2 Load External TsFile Tool + +For more details, see document [Data Import](../Tools-System/Data-Import-Tool.md). + +#### Load with SQL + +1. Load a single tsfile by specifying a file path (absolute path). + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +2. Load a batch of files by specifying a folder path (absolute path). + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +#### Load with Script + +``` +./load-rewrite.bat -f D:\IoTDB\data -h 192.168.0.101 -p 6667 -u root -pw root +``` + +## 5. DELETE DATA + +For more details, see document [Write-Delete-Data](../Basic-Concept/Write-Data). + +### 5.1 Delete Single Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time < 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time > 20 +IoTDB > delete from root.ln.wf02.wt02.status where time >= 20 +IoTDB > delete from root.ln.wf02.wt02.status where time = 20 +IoTDB > delete from root.ln.wf02.wt02.status where time > 4 or time < 0 +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +IoTDB > delete from root.ln.wf02.wt02.status +``` + +### 5.2 Delete Multiple Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +IoTDB> delete from root.ln.wf03.wt02.status where time < now() +Msg: The statement is executed successfully. +``` + +### 5.3 Delete Time Partition (experimental) + +```sql +IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +## 6. QUERY DATA + +For more details, see document [Query-Data](../Basic-Concept/Query-Data.md). + +```sql +SELECT [LAST] selectExpr [, selectExpr] ... + [INTO intoItem [, intoItem] ...] + FROM prefixPath [, prefixPath] ... + [WHERE whereCondition] + [GROUP BY { + ([startTime, endTime), interval [, slidingStep]) | + LEVEL = levelNum [, levelNum] ... | + TAGS(tagKey [, tagKey] ... ) | + VARIATION(expression[,delta][,ignoreNull=true/false]) | + CONDITION(expression,[keep>/>=/=/ select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` + +#### Select Multiple Columns of Data Based on a Time Interval + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals + +```sql +IoTDB > select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals + +```sql +IoTDB > select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Order By Time Query + +```sql +IoTDB > select * from root.ln.** where time > 1 order by time desc limit 10; +``` + +### 6.2 `SELECT` CLAUSE + +#### Use Alias + +```sql +IoTDB > select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` + +#### Nested Expressions + +##### Nested Expressions with Time Series Query + +```sql +IoTDB > select a, + b, + ((a + 1) * 2 - 1) % 2 + 1.5, + sin(a + sin(a + sin(b))), + -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 +from root.sg1; + +IoTDB > select (a + b) * 2 + sin(a) from root.sg + +IoTDB > select (a + *) / 2 from root.sg1 + +IoTDB > select (a + b) * 3 from root.sg, root.ln +``` + +##### Nested Expressions query with aggregations + +```sql +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) +from root.ln.wf01.wt01; + +IoTDB > select avg(*), + (avg(*) + 1) * 3 / 2 -1 +from root.sg1 + +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) as custom_sum +from root.ln.wf01.wt01 +GROUP BY([10, 90), 10ms); +``` + +#### Last Query + +```sql +IoTDB > select last status from root.ln.wf01.wt01 +IoTDB > select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +IoTDB > select last * from root.ln.wf01.wt01 order by timeseries desc; +IoTDB > select last * from root.ln.wf01.wt01 order by dataType desc; +``` + +### 6.3 `WHERE` CLAUSE + +#### Time Filter + +```sql +IoTDB > select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Value Filter + +```sql +IoTDB > select temperature from root.sg1.d1 where temperature > 36.5; +IoTDB > select status from root.sg1.d1 where status = true; +IoTDB > select temperature from root.sg1.d1 where temperature between 36.5 and 40; +IoTDB > select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +IoTDB > select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where temperature is null; +IoTDB > select code from root.sg1.d1 where temperature is not null; +``` + +#### Fuzzy Query + +- Fuzzy matching using `Like` + +```sql +IoTDB > select * from root.sg.d1 where value like '%cc%' +IoTDB > select * from root.sg.device where value like '_b_' +``` + +- Fuzzy matching using `Regexp` + +```sql +IoTDB > select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +IoTDB > select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 6.4 `GROUP BY` CLAUSE + +- Aggregate By Time without Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` + +- Aggregate By Time Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` + +- Aggregate by Natural Month + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` + +- Left Open And Right Close Range + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` + +- Aggregation By Variation + +```sql +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6+s5, 10) +``` + +- Aggregation By Condition + +```sql +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=true) +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=false) +``` + +- Aggregation By Session + +```sql +IoTDB > select __endTime,count(*) from root.** group by session(1d) +IoTDB > select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` + +- Aggregation By Count + +```sql +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` + +- Aggregation By Level + +```sql +IoTDB > select count(status) from root.** group by level = 1 +IoTDB > select count(status) from root.** group by level = 3 +IoTDB > select count(status) from root.** group by level = 1, 3 +IoTDB > select max_value(temperature) from root.** group by level = 0 +IoTDB > select count(*) from root.ln.** group by level = 2 +``` + +- Aggregate By Time with Level Clause + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +IoTDB > select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` + +- Aggregation query by one single tag + +```sql +IoTDB > SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` + +- Aggregation query by multiple tags + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` + +- Downsampling Aggregation by tags based on Time Window + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` + +### 6.5 `HAVING` CLAUSE + +Correct: + +```sql +IoTDB > select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 1 +IoTDB > select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device +``` + +Incorrect: + +```sql +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 +IoTDB > select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` + +### 6.6 `FILL` CLAUSE + +#### `PREVIOUS` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` + +#### `PREVIOUS` FILL and specify the fill timeout threshold +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` + +#### `LINEAR` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` + +#### Constant Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` + +### 6.7 `LIMIT` and `SLIMIT` CLAUSES (PAGINATION) + +#### Row Control over Query Results + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 10 +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +IoTDB > select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 2 offset 3 +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 5 offset 3 +``` + +#### Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +IoTDB > select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` + +#### Row and Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` + +### 6.8 `ORDER BY` CLAUSE + +#### Order by in ALIGN BY TIME mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` + +#### Order by in ALIGN BY DEVICE mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +IoTDB > select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` + +#### Order by arbitrary expressions + +```sql +IoTDB > select score from root.** order by score desc align by device +IoTDB > select score,total from root.one order by base+score+bonus desc +IoTDB > select score,total from root.one order by total desc +IoTDB > select base, score, bonus, total from root.** order by total desc NULLS Last, + score desc NULLS Last, + bonus desc NULLS Last, + time desc align by device +IoTDB > select min_value(total) from root.** order by min_value(total) asc align by device +IoTDB > select min_value(total),max_value(base) from root.** order by max_value(total) desc align by device +IoTDB > select score from root.** order by device asc, score desc, time asc align by device +``` + +### 6.9 `ALIGN BY` CLAUSE + +#### Align by Device + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` + +### 6.10 `INTO` CLAUSE (QUERY WRITE-BACK) + +```sql +IoTDB > select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +IoTDB > select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +IoTDB > select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` + +- Using variable placeholders: + +```sql +IoTDB > select s1, s2 +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) +from root.sg.d1, root.sg.d2; + +IoTDB > select d1.s1, d1.s2, d2.s3, d3.s4 +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) +from root.sg; + +IoTDB > select * into root.sg_bk.::(::) from root.sg.**; + +IoTDB > select s1, s2, s3, s4 +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) +from root.sg.d1, root.sg.d2, root.sg.d3 +align by device; + +IoTDB > select avg(s1), sum(s2) + sum(s3), count(s4) +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) +from root.** +align by device; + +IoTDB > select * into ::(backup_${4}) from root.sg.** align by device; + +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` + +## 7. Maintennance +Generate the corresponding query plan: +``` +explain select s1,s2 from root.sg.d1 +``` +Execute the corresponding SQL, analyze the execution and output: +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 8. OPERATOR + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 8.1 Arithmetic Operators + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-operators). + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 8.2 Comparison Operators + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-operators). + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 8.3 Logical Operators + +For details and examples, see the document [Logical Operators](./Operator-and-Expression.md#logical-operators). + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 9. BUILT-IN FUNCTIONS + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md#built-in-functions). + +### 9.1 Aggregate Functions + +For details and examples, see the document [Aggregate Functions](./Operator-and-Expression.md#aggregate-functions). + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 9.2 Arithmetic Functions + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-functions). + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 9.3 Comparison Functions + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-functions). + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 9.4 String Processing Functions + +For details and examples, see the document [String Processing](./Operator-and-Expression.md#string-processing-functions). + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 9.5 Data Type Conversion Function + +For details and examples, see the document [Data Type Conversion Function](./Operator-and-Expression.md#data-type-conversion-function). + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 9.6 Constant Timeseries Generating Functions + +For details and examples, see the document [Constant Timeseries Generating Functions](./Operator-and-Expression.md#constant-timeseries-generating-functions). + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 9.7 Selector Functions + +For details and examples, see the document [Selector Functions](./Operator-and-Expression.md#selector-functions). + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 9.8 Continuous Interval Functions + +For details and examples, see the document [Continuous Interval Functions](./Operator-and-Expression.md#continuous-interval-functions). + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 9.9 Variation Trend Calculation Functions + +For details and examples, see the document [Variation Trend Calculation Functions](./Operator-and-Expression.md#variation-trend-calculation-functions). + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 9.10 Sample Functions + +For details and examples, see the document [Sample Functions](./Operator-and-Expression.md#sample-functions). + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 9.11 Change Points Function + +For details and examples, see the document [Time-Series](./Operator-and-Expression.md#change-points-function). + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 10. DATA QUALITY FUNCTION LIBRARY + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 10.1 Data Quality + +For details and examples, see the document [Data-Quality](../SQL-Manual/UDF-Libraries.md#data-quality). + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 10.2 Data Profiling + +For details and examples, see the document [Data-Profiling](../SQL-Manual/UDF-Libraries.md#data-profiling). + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 10.3 Anomaly Detection + +For details and examples, see the document [Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#anomaly-detection). + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 10.4 Frequency Domain + +For details and examples, see the document [Frequency-Domain](../SQL-Manual/UDF-Libraries.md#frequency-domain-analysis). + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 10.5 Data Matching + +For details and examples, see the document [Data-Matching](../SQL-Manual/UDF-Libraries.md#data-matching). + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 10.6 Data Repairing + +For details and examples, see the document [Data-Repairing](../SQL-Manual/UDF-Libraries.md#data-repairing). + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 10.7 Series Discovery + +For details and examples, see the document [Series-Discovery](../SQL-Manual/UDF-Libraries.md#series-discovery). + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 10.8 Machine Learning + +For details and examples, see the document [Machine-Learning](../SQL-Manual/UDF-Libraries.md#machine-learning). + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 11. LAMBDA EXPRESSION + +For details and examples, see the document [Lambda](../SQL-Manual/UDF-Libraries.md#lambda-expression). + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 12. CONDITIONAL EXPRESSION + +For details and examples, see the document [Conditional Expressions](../SQL-Manual/UDF-Libraries.md#conditional-expressions). + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +### 14.1 Configuring execution intervals + +```sql +CREATE CONTINUOUS QUERY cq1 +RESAMPLE EVERY 20s +BEGIN +SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 14.2 Configuring time range for resampling + +```sql +CREATE CONTINUOUS QUERY cq2 +RESAMPLE RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 14.3 Configuring execution intervals and CQ time ranges + +```sql +CREATE CONTINUOUS QUERY cq3 +RESAMPLE EVERY 20s RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 14.4 Configuring end_time_offset for CQ time range + +```sql +CREATE CONTINUOUS QUERY cq4 +RESAMPLE EVERY 20s RANGE 40s, 20s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 14.5 CQ without group by clause + +```sql +CREATE CONTINUOUS QUERY cq5 +RESAMPLE EVERY 20s +BEGIN + SELECT temperature + 1 + INTO root.precalculated_sg.::(temperature) + FROM root.ln.*.* + align by device +END +``` + +### 14.6 CQ Management + +#### Listing continuous queries + +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` + +#### Dropping continuous queries + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` + +#### Altering continuous queries + +CQs can't be altered once they're created. To change a CQ, you must `DROP` and re`CREATE` it with the updated settings. + +## 15. USER-DEFINED FUNCTION (UDF) + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 15.1 UDF Registration + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +### 15.2 UDF Deregistration + +```sql +DROP FUNCTION +``` + +### 15.3 UDF Queries + +```sql +SELECT example(*) from root.sg.d1 +SELECT example(s1, *) from root.sg.d1 +SELECT example(*, *) from root.sg.d1 + +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; + +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` + +### 15.4 Show All Registered UDFs + +```sql +SHOW FUNCTIONS +``` + +## 16. ADMINISTRATION MANAGEMENT + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 16.1 SQL Statements + +- Create user (Requires MANAGE_USER permission) + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- Delete user (Requires MANAGE_USER permission) + +```sql +DROP USER +eg: DROP USER user1 +``` + +- Create role (Requires MANAGE_ROLE permission) + +```sql +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- Delete role (Requires MANAGE_ROLE permission) + +```sql +DROP ROLE +eg: DROP ROLE role1 +``` + +- Grant role to user (Requires MANAGE_ROLE permission) + +```sql +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- Revoke role from user(Requires MANAGE_ROLE permission) + +```sql +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- List all user (Requires MANAGE_USER permission) + +```sql +LIST USER +``` + +- List all role (Requires MANAGE_ROLE permission) + +```sql +LIST ROLE +``` + +- List all users granted specific role.(Requires MANAGE_USER permission) + +```sql +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- List all role granted to specific user. + +```sql +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- List all privileges of user + +```sql +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; +``` + +- List all privileges of role + +```sql +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- Update password + +```sql +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 16.2 Authorization and Deauthorization + + +```sql +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +```sql +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + + +#### Delete Time Partition (experimental) + +``` +Eg: IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +#### Continuous Query,CQ + +``` +Eg: IoTDB > CREATE CONTINUOUS QUERY cq1 BEGIN SELECT max_value(temperature) INTO temperature_max FROM root.ln.*.* GROUP BY time(10s) END +``` + +#### Maintenance Command + +- FLUSH + +``` +Eg: IoTDB > flush +``` + +- MERGE + +``` +Eg: IoTDB > MERGE +Eg: IoTDB > FULL MERGE +``` + +- CLEAR CACHE + +```sql +Eg: IoTDB > CLEAR CACHE +``` + +- START REPAIR DATA + +```sql +Eg: IoTDB > START REPAIR DATA +``` + +- STOP REPAIR DATA + +```sql +Eg: IoTDB > STOP REPAIR DATA +``` + +- SET SYSTEM TO READONLY / WRITABLE + +``` +Eg: IoTDB > SET SYSTEM TO READONLY / WRITABLE +``` + +- Query abort + +``` +Eg: IoTDB > KILL QUERY 1 +``` \ No newline at end of file diff --git a/src/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md b/src/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md index 6a7d90b4d..680c9577a 100644 --- a/src/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md +++ b/src/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md @@ -349,247 +349,9 @@ IoTDB> show devices All devices will definitely have a TTL, meaning it cannot be null. INF represents infinity. -## 2. Device Template +## 2. Timeseries Management -IoTDB supports the device template function, enabling different entities of the same type to share metadata, reduce the memory usage of metadata, and simplify the management of numerous entities and measurements. - - -### 2.1 Create Device Template - -The SQL syntax for creating a metadata template is as follows: - -```sql -CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' -``` - -**Example 1:** Create a template containing two non-aligned timeseries - -```shell -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) -``` - -**Example 2:** Create a template containing a group of aligned timeseries - -```shell -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` - -The` lat` and `lon` measurements are aligned. - -When creating a template, the system will automatically assign default encoding and compression methods, requiring no manual specification. If your business scenario requires custom adjustments, you may refer to the following example: - -```shell -IoTDB> create device template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY) -``` -For a full list of supported data types and corresponding encoding methods, please refer to [Compression & Encoding](../Technical-Insider/Encoding-and-Compression.md)。 - -![](/img/%E6%A8%A1%E6%9D%BF.png) - -![](/img/templateEN.jpg) - -### 2.2 Set Device Template - -After a device template is created, it should be set to specific path before creating related timeseries or insert data. - -**It should be ensured that the related database has been set before setting template.** - -**It is recommended to set device template to database path. It is not suggested to set device template to some path above database** - -**It is forbidden to create timeseries under a path setting s tedeviceplate. Device template shall not be set on a prefix path of an existing timeseries.** - -The SQL Statement for setting device template is as follow: - -```shell -IoTDB> set device template t1 to root.sg1.d1 -``` - -### 2.3 Activate Device Template - -After setting the device template, with the system enabled to auto create schema, you can insert data into the timeseries. For example, suppose there's a database root.sg1 and t1 has been set to root.sg1.d1, then timeseries like root.sg1.d1.temperature and root.sg1.d1.status are available and data points can be inserted. - - -**Attention**: Before inserting data or the system not enabled to auto create schema, timeseries defined by the device template will not be created. You can use the following SQL statement to create the timeseries or activate the templdeviceate, act before inserting data: - -```shell -IoTDB> create timeseries using device template on root.sg1.d1 -``` - -**Example:** Execute the following statement - -```shell -IoTDB> set device template t1 to root.sg1.d1 -IoTDB> set device template t2 to root.sg1.d2 -IoTDB> create timeseries using device template on root.sg1.d1 -IoTDB> create timeseries using device template on root.sg1.d2 -``` - -Show the time series: - -```sql -show timeseries root.sg1.** -```` - -```shell -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression|tags|attributes|deadband|deadband parameters| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -|root.sg1.d1.temperature| null| root.sg1| FLOAT| RLE| SNAPPY|null| null| null| null| -| root.sg1.d1.status| null| root.sg1| BOOLEAN| PLAIN| SNAPPY|null| null| null| null| -| root.sg1.d2.lon| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -| root.sg1.d2.lat| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -``` - -Show the devices: - -```sql -show devices root.sg1.** -```` - -```shell -+---------------+---------+ -| devices|isAligned| -+---------------+---------+ -| root.sg1.d1| false| -| root.sg1.d2| true| -+---------------+---------+ -```` - -### 2.4 Show Device Template - -- Show all device templates - -The SQL statement looks like this: - -```shell -IoTDB> show device templates -``` - -The execution result is as follows: - -```shell -+-------------+ -|template name| -+-------------+ -| t2| -| t1| -+-------------+ -``` - -- Show nodes under in device template - -The SQL statement looks like this: - -```shell -IoTDB> show nodes in device template t1 -``` - -The execution result is as follows: - -```shell -+-----------+--------+--------+-----------+ -|child nodes|dataType|encoding|compression| -+-----------+--------+--------+-----------+ -|temperature| FLOAT| RLE| SNAPPY| -| status| BOOLEAN| PLAIN| SNAPPY| -+-----------+--------+--------+-----------+ -``` - -- Show the path prefix where a device template is set - -```shell -IoTDB> show paths set device template t1 -``` - -The execution result is as follows: - -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -- Show the path prefix where a device template is used (i.e. the time series has been created) - -```shell -IoTDB> show paths using device template t1 -``` - -The execution result is as follows: - -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -### 2.5 Deactivate device Template - -To delete a group of timeseries represented by device template, namely deactivate the device template, use the following SQL statement: - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -``` - -or - -```shell -IoTDB> deactivate device template t1 from root.sg1.d1 -``` - -The deactivation supports batch process. - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -``` - -or - -```shell -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` - -If the template name is not provided in sql, all template activation on paths matched by given path pattern will be removed. - -### 2.6 Unset Device Template - -The SQL Statement for unsetting device template is as follow: - -```shell -IoTDB> unset device template t1 from root.sg1.d1 -``` - -**Attention**: It should be guaranteed that none of the timeseries represented by the target device template exists, before unset it. It can be achieved by deactivation operation. - -### 2.7 Drop Device Template - -The SQL Statement for dropping device template is as follow: - -```shell -IoTDB> drop device template t1 -``` - -**Attention**: Dropping an already set template is not supported. - -### 2.8 Alter Device Template - -In a scenario where measurements need to be added, you can modify the template to add measurements to all devicesdevice using the device template. - -The SQL Statement for altering device template is as follow: - -```shell -IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) -``` - -**When executing data insertion to devices with device template set on related prefix path and there are measurements not present in this device template, the measurements will be auto added to this device template.** - -## 3. Timeseries Management - -### 3.1 Create Timeseries +### 2.1 Create Timeseries According to the storage model selected before, we can create corresponding timeseries in the two databases respectively. The SQL statements for creating timeseries are as follows: @@ -629,7 +391,7 @@ error: encoding TS_2DIFF does not support BOOLEAN For a full list of supported data types and corresponding encoding methods, please refer to [Compression & Encoding](../Technical-Insider/Encoding-and-Compression.md)。 -### 3.2 Create Aligned Timeseries +### 2.2 Create Aligned Timeseries The SQL statement for creating a group of timeseries are as follows: @@ -641,7 +403,7 @@ You can set different datatype, encoding, and compression for the timeseries in It is also supported to set an alias, tag, and attribute for aligned timeseries. -### 3.3 Delete Timeseries +### 2.3 Delete Timeseries To delete the timeseries we created before, we are able to use `(DELETE | DROP) TimeSeries ` statement. @@ -654,7 +416,7 @@ IoTDB> delete timeseries root.ln.wf02.* IoTDB> drop timeseries root.ln.wf02.* ``` -### 3.4 Show Timeseries +### 2.4 Show Timeseries * SHOW LATEST? TIMESERIES pathPattern? whereClause? limitClause? @@ -798,7 +560,7 @@ It costs 0.004s It is worth noting that when the queried path does not exist, the system will return no timeseries. -### 3.5 Count Timeseries +### 2.5 Count Timeseries IoTDB is able to use `COUNT TIMESERIES ` to count the number of timeseries matching the path. SQL statements are as follows: @@ -883,7 +645,7 @@ It costs 0.002s > Note: The path of timeseries is just a filter condition, which has no relationship with the definition of level. -### 3.6 Active Timeseries Query +### 2.6 Active Timeseries Query By adding WHERE time filter conditions to the existing SHOW/COUNT TIMESERIES, we can obtain time series with data within the specified time range. It is important to note that in metadata queries with time filters, views are not considered; only the time series actually stored in the TsFile are taken into account. @@ -923,7 +685,7 @@ IoTDB> count timeseries where time >= 15000 and time < 16000; +-----------------+ ``` Regarding the definition of active time series, data that can be queried normally is considered active, meaning time series that have been inserted but deleted are not included. -### 3.7 Tag and Attribute Management +### 2.7 Tag and Attribute Management We can also add an alias, extra tag and attribute information while creating one timeseries. @@ -1098,9 +860,9 @@ IoTDB> show timeseries where TAGS(tag1)='v1' The above operations are supported for timeseries tag, attribute updates, etc. -## 4. Path query +## 3. Path query -### 4.1 Path +### 3.1 Path A `path` is an expression that conforms to the following constraints: @@ -1120,7 +882,7 @@ wildcard ; ``` -### 4.2 NodeName +### 3.2 NodeName - The parts of a path separated by `.` are called node names (`nodeName`). - For example, `root.a.b.c` is a path with a depth of 4 levels, where `root`, `a`, `b`, and `c` are all node names. @@ -1135,11 +897,11 @@ wildcard - UNICODE Chinese characters (`\u2E80` to `\u9FFF`) - **Case sensitivity**: On Windows systems, path node names in the database are case-insensitive. For example, `root.ln` and `root.LN` are considered the same path. -### 4.3 Special Characters (Backquote) +### 3.3 Special Characters (Backquote) If special characters (such as spaces or punctuation marks) are needed in a `nodeName`, you can enclose the node name in Backquote (`). For more information on the use of backticks, please refer to [Backquote](../SQL-Manual/Syntax-Rule.md#reverse-quotation-marks). -### 4.4 Path Pattern +### 3.4 Path Pattern To make it more convenient and efficient to express multiple time series, IoTDB provides paths with wildcards `*` and `**`. Wildcards can appear in any level of a path. @@ -1152,7 +914,7 @@ To make it more convenient and efficient to express multiple time series, IoTDB **Note**: `*` and `**` cannot be placed at the beginning of a path. -### 4.5 Show Child Paths +### 3.5 Show Child Paths ``` SHOW CHILD PATHS pathPattern @@ -1180,7 +942,7 @@ It costs 0.002s > get all paths in form of root.xx.xx.xx:show child paths root.xx.xx -### 4.6 Show Child Nodes +### 3.6 Show Child Nodes ``` SHOW CHILD NODES pathPattern @@ -1211,7 +973,7 @@ Example: +------------+ ``` -### 4.7 Count Nodes +### 3.7 Count Nodes IoTDB is able to use `COUNT NODES LEVEL=` to count the number of nodes at the given level in current Metadata Tree considering a given pattern. IoTDB will find paths that @@ -1264,7 +1026,7 @@ It costs 0.002s > Note: The path of timeseries is just a filter condition, which has no relationship with the definition of level. -### 4.8 Show Devices +### 3.8 Show Devices * SHOW DEVICES pathPattern? (WITH DATABASE)? devicesWhereClause? limitClause? @@ -1345,7 +1107,7 @@ Total line number = 2 It costs 0.001s ``` -### 4.9 Count Devices +### 3.9 Count Devices * COUNT DEVICES / @@ -1390,7 +1152,7 @@ Total line number = 1 It costs 0.004s ``` -### 4.10 Active Device Query +### 3.10 Active Device Query Similar to active timeseries query, we can add time filter conditions to device viewing and statistics to query active devices that have data within a certain time range. The definition of active here is the same as for active time series. An example usage is as follows: ``` IoTDB> insert into root.sg.data(timestamp, s1,s2) values(15000, 1, 2); diff --git a/src/UserGuide/latest/SQL-Manual/SQL-Manual-timecho.md b/src/UserGuide/latest/SQL-Manual/SQL-Manual-timecho.md new file mode 100644 index 000000000..3c197fdca --- /dev/null +++ b/src/UserGuide/latest/SQL-Manual/SQL-Manual-timecho.md @@ -0,0 +1,1681 @@ + + +# SQL Manual + +## 1. DATABASE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 1.1 Create Database + +```sql +IoTDB > create database root.ln +IoTDB > create database root.sgcc +``` + +### 1.2 Show Databases + +```sql +IoTDB> SHOW DATABASES +IoTDB> SHOW DATABASES root.** +``` + +### 1.3 Delete Database + +```sql +IoTDB > DELETE DATABASE root.ln +IoTDB > DELETE DATABASE root.sgcc +// delete all data, all timeseries and all databases +IoTDB > DELETE DATABASE root.** +``` + +### 1.4 Count Databases + +```sql +IoTDB> count databases +IoTDB> count databases root.* +IoTDB> count databases root.sgcc.* +IoTDB> count databases root.sgcc +``` + +### 1.5 Setting up heterogeneous databases (Advanced operations) + +#### Set heterogeneous parameters when creating a Database + +```sql +CREATE DATABASE root.db WITH SCHEMA_REPLICATION_FACTOR=1, DATA_REPLICATION_FACTOR=3, SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Adjust heterogeneous parameters at run time + +```sql +ALTER DATABASE root.db WITH SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Show heterogeneous databases + +```sql +SHOW DATABASES DETAILS +``` + +### 1.6 TTL + +#### Set TTL + +```sql +IoTDB> set ttl to root.ln 3600000 +IoTDB> set ttl to root.sgcc.** 3600000 +IoTDB> set ttl to root.** 3600000 +``` + +#### Unset TTL + +```sql +IoTDB> unset ttl from root.ln +IoTDB> unset ttl from root.sgcc.** +IoTDB> unset ttl from root.** +``` + +#### Show TTL + +```sql +IoTDB> SHOW ALL TTL +IoTDB> SHOW TTL ON StorageGroupNames +IoTDB> SHOW DEVICES +``` + + +## 2. TIMESERIES MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 2.1 Create Timeseries + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- From v0.13, you can use a simplified version of the SQL statements to create timeseries: + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- Notice that when in the CREATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below: + +```sql +IoTDB > create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +error: encoding TS_2DIFF does not support BOOLEAN +``` + +### 2.2 Create Aligned Timeseries + +```sql +IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT , longitude FLOAT) +``` + +### 2.3 Delete Timeseries + +```sql +IoTDB> delete timeseries root.ln.wf01.wt01.status +IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +IoTDB> delete timeseries root.ln.wf02.* +IoTDB> drop timeseries root.ln.wf02.* +``` + +### 2.4 Show Timeseries + +```sql +IoTDB> show timeseries root.** +IoTDB> show timeseries root.ln.** +IoTDB> show timeseries root.ln.** limit 10 offset 10 +IoTDB> show timeseries root.ln.** where timeseries contains 'wf01.wt' +IoTDB> show timeseries root.ln.** where dataType=FLOAT +``` + +### 2.5 Count Timeseries + +```sql +IoTDB > COUNT TIMESERIES root.** +IoTDB > COUNT TIMESERIES root.ln.** +IoTDB > COUNT TIMESERIES root.ln.*.*.status +IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +IoTDB > COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1 +IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +### 2.6 Tag and Attribute Management + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +* Rename the tag/attribute key + +```SQL +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +* Reset the tag/attribute value + +```SQL +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +* Delete the existing tag/attribute + +```SQL +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +* Add new tags + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +* Add new attributes + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +* Upsert alias, tags and attributes + +> add alias or a new key-value if the alias or key doesn't exist, otherwise, update the old one with new value. + +```SQL +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +* Show timeseries using tags. Use TAGS(tagKey) to identify the tags used as filter key + +```SQL +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +returns all the timeseries information that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- count timeseries using tags + +```SQL +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +returns all the number of timeseries that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +create aligned timeseries + +```SQL +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +The execution result is as follows: + +```SQL +IoTDB> show timeseries ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| +|root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +Support query: + +```SQL +IoTDB> show timeseries where TAGS(tag1)='v1' ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +The above operations are supported for timeseries tag, attribute updates, etc. + +## 3. NODE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 3.1 Show Child Paths + +```SQL +SHOW CHILD PATHS pathPattern +``` + +### 3.2 Show Child Nodes + +```SQL +SHOW CHILD NODES pathPattern +``` + +### 3.3 Count Nodes + +```SQL +IoTDB > COUNT NODES root.** LEVEL=2 +IoTDB > COUNT NODES root.ln.** LEVEL=2 +IoTDB > COUNT NODES root.ln.wf01.** LEVEL=3 +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` + +### 3.4 Show Devices + +```SQL +IoTDB> show devices +IoTDB> show devices root.ln.** +IoTDB> show devices root.ln.** where device contains 't' +IoTDB> show devices with database +IoTDB> show devices root.ln.** with database +``` + +### 3.5 Count Devices + +```SQL +IoTDB> show devices +IoTDB> count devices +IoTDB> count devices root.ln.** +``` + +## 4. INSERT & LOAD DATA + +### 4.1 Insert Data + +For more details, see document [Write-Data](../Basic-Concept/Write-Data). + +#### Use of INSERT Statements + +- Insert Single Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1') +``` + +- Insert Multiple Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (2, false, 'v2') +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` + +- Use the Current System Timestamp as the Timestamp of the Data Point + +```SQL +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` + +#### Insert Data Into Aligned Timeseries + +```SQL +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(1, 1, 1) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +IoTDB > select * from root.sg1.d1 +``` + +### 4.2 Load External TsFile Tool + +For more details, see document [Data Import](../Tools-System/Data-Import-Tool.md). + +#### Load with SQL + +1. Load a single tsfile by specifying a file path (absolute path). + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +2. Load a batch of files by specifying a folder path (absolute path). + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +#### Load with Script + +``` +./load-rewrite.bat -f D:\IoTDB\data -h 192.168.0.101 -p 6667 -u root -pw root +``` + +## 5. DELETE DATA + +For more details, see document [Write-Delete-Data](../Basic-Concept/Write-Data). + +### 5.1 Delete Single Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time < 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time > 20 +IoTDB > delete from root.ln.wf02.wt02.status where time >= 20 +IoTDB > delete from root.ln.wf02.wt02.status where time = 20 +IoTDB > delete from root.ln.wf02.wt02.status where time > 4 or time < 0 +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +IoTDB > delete from root.ln.wf02.wt02.status +``` + +### 5.2 Delete Multiple Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +IoTDB> delete from root.ln.wf03.wt02.status where time < now() +Msg: The statement is executed successfully. +``` + +### 5.3 Delete Time Partition (experimental) + +```sql +IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +## 6. QUERY DATA + +For more details, see document [Query-Data](../Basic-Concept/Query-Data.md). + +```sql +SELECT [LAST] selectExpr [, selectExpr] ... + [INTO intoItem [, intoItem] ...] + FROM prefixPath [, prefixPath] ... + [WHERE whereCondition] + [GROUP BY { + ([startTime, endTime), interval [, slidingStep]) | + LEVEL = levelNum [, levelNum] ... | + TAGS(tagKey [, tagKey] ... ) | + VARIATION(expression[,delta][,ignoreNull=true/false]) | + CONDITION(expression,[keep>/>=/=/ select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` + +#### Select Multiple Columns of Data Based on a Time Interval + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals + +```sql +IoTDB > select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals + +```sql +IoTDB > select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Order By Time Query + +```sql +IoTDB > select * from root.ln.** where time > 1 order by time desc limit 10; +``` + +### 6.2 `SELECT` CLAUSE + +#### Use Alias + +```sql +IoTDB > select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` + +#### Nested Expressions + +##### Nested Expressions with Time Series Query + +```sql +IoTDB > select a, + b, + ((a + 1) * 2 - 1) % 2 + 1.5, + sin(a + sin(a + sin(b))), + -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 +from root.sg1; + +IoTDB > select (a + b) * 2 + sin(a) from root.sg + +IoTDB > select (a + *) / 2 from root.sg1 + +IoTDB > select (a + b) * 3 from root.sg, root.ln +``` + +##### Nested Expressions query with aggregations + +```sql +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) +from root.ln.wf01.wt01; + +IoTDB > select avg(*), + (avg(*) + 1) * 3 / 2 -1 +from root.sg1 + +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) as custom_sum +from root.ln.wf01.wt01 +GROUP BY([10, 90), 10ms); +``` + +#### Last Query + +```sql +IoTDB > select last status from root.ln.wf01.wt01 +IoTDB > select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +IoTDB > select last * from root.ln.wf01.wt01 order by timeseries desc; +IoTDB > select last * from root.ln.wf01.wt01 order by dataType desc; +``` + +### 6.3 `WHERE` CLAUSE + +#### Time Filter + +```sql +IoTDB > select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Value Filter + +```sql +IoTDB > select temperature from root.sg1.d1 where temperature > 36.5; +IoTDB > select status from root.sg1.d1 where status = true; +IoTDB > select temperature from root.sg1.d1 where temperature between 36.5 and 40; +IoTDB > select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +IoTDB > select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where temperature is null; +IoTDB > select code from root.sg1.d1 where temperature is not null; +``` + +#### Fuzzy Query + +- Fuzzy matching using `Like` + +```sql +IoTDB > select * from root.sg.d1 where value like '%cc%' +IoTDB > select * from root.sg.device where value like '_b_' +``` + +- Fuzzy matching using `Regexp` + +```sql +IoTDB > select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +IoTDB > select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 6.4 `GROUP BY` CLAUSE + +- Aggregate By Time without Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` + +- Aggregate By Time Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` + +- Aggregate by Natural Month + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` + +- Left Open And Right Close Range + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` + +- Aggregation By Variation + +```sql +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6+s5, 10) +``` + +- Aggregation By Condition + +```sql +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=true) +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=false) +``` + +- Aggregation By Session + +```sql +IoTDB > select __endTime,count(*) from root.** group by session(1d) +IoTDB > select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` + +- Aggregation By Count + +```sql +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` + +- Aggregation By Level + +```sql +IoTDB > select count(status) from root.** group by level = 1 +IoTDB > select count(status) from root.** group by level = 3 +IoTDB > select count(status) from root.** group by level = 1, 3 +IoTDB > select max_value(temperature) from root.** group by level = 0 +IoTDB > select count(*) from root.ln.** group by level = 2 +``` + +- Aggregate By Time with Level Clause + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +IoTDB > select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` + +- Aggregation query by one single tag + +```sql +IoTDB > SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` + +- Aggregation query by multiple tags + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` + +- Downsampling Aggregation by tags based on Time Window + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` + +### 6.5 `HAVING` CLAUSE + +Correct: + +```sql +IoTDB > select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 1 +IoTDB > select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device +``` + +Incorrect: + +```sql +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 +IoTDB > select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` + +### 6.6 `FILL` CLAUSE + +#### `PREVIOUS` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` + +#### `PREVIOUS` FILL and specify the fill timeout threshold +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` + +#### `LINEAR` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` + +#### Constant Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` + +### 6.7 `LIMIT` and `SLIMIT` CLAUSES (PAGINATION) + +#### Row Control over Query Results + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 10 +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +IoTDB > select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 2 offset 3 +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 5 offset 3 +``` + +#### Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +IoTDB > select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` + +#### Row and Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` + +### 6.8 `ORDER BY` CLAUSE + +#### Order by in ALIGN BY TIME mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` + +#### Order by in ALIGN BY DEVICE mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +IoTDB > select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` + +#### Order by arbitrary expressions + +```sql +IoTDB > select score from root.** order by score desc align by device +IoTDB > select score,total from root.one order by base+score+bonus desc +IoTDB > select score,total from root.one order by total desc +IoTDB > select base, score, bonus, total from root.** order by total desc NULLS Last, + score desc NULLS Last, + bonus desc NULLS Last, + time desc align by device +IoTDB > select min_value(total) from root.** order by min_value(total) asc align by device +IoTDB > select min_value(total),max_value(base) from root.** order by max_value(total) desc align by device +IoTDB > select score from root.** order by device asc, score desc, time asc align by device +``` + +### 6.9 `ALIGN BY` CLAUSE + +#### Align by Device + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` + +### 6.10 `INTO` CLAUSE (QUERY WRITE-BACK) + +```sql +IoTDB > select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +IoTDB > select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +IoTDB > select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` + +- Using variable placeholders: + +```sql +IoTDB > select s1, s2 +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) +from root.sg.d1, root.sg.d2; + +IoTDB > select d1.s1, d1.s2, d2.s3, d3.s4 +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) +from root.sg; + +IoTDB > select * into root.sg_bk.::(::) from root.sg.**; + +IoTDB > select s1, s2, s3, s4 +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) +from root.sg.d1, root.sg.d2, root.sg.d3 +align by device; + +IoTDB > select avg(s1), sum(s2) + sum(s3), count(s4) +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) +from root.** +align by device; + +IoTDB > select * into ::(backup_${4}) from root.sg.** align by device; + +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` + +## 7. Maintennance +Generate the corresponding query plan: +``` +explain select s1,s2 from root.sg.d1 +``` +Execute the corresponding SQL, analyze the execution and output: +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 8. OPERATOR + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 8.1 Arithmetic Operators + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-operators). + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 8.2 Comparison Operators + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-operators). + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 8.3 Logical Operators + +For details and examples, see the document [Logical Operators](./Operator-and-Expression.md#logical-operators). + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 9. BUILT-IN FUNCTIONS + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md#built-in-functions). + +### 9.1 Aggregate Functions + +For details and examples, see the document [Aggregate Functions](./Operator-and-Expression.md#aggregate-functions). + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 9.2 Arithmetic Functions + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-functions). + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 9.3 Comparison Functions + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-functions). + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 9.4 String Processing Functions + +For details and examples, see the document [String Processing](./Operator-and-Expression.md#string-processing-functions). + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 9.5 Data Type Conversion Function + +For details and examples, see the document [Data Type Conversion Function](./Operator-and-Expression.md#data-type-conversion-function). + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 9.6 Constant Timeseries Generating Functions + +For details and examples, see the document [Constant Timeseries Generating Functions](./Operator-and-Expression.md#constant-timeseries-generating-functions). + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 9.7 Selector Functions + +For details and examples, see the document [Selector Functions](./Operator-and-Expression.md#selector-functions). + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 9.8 Continuous Interval Functions + +For details and examples, see the document [Continuous Interval Functions](./Operator-and-Expression.md#continuous-interval-functions). + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 9.9 Variation Trend Calculation Functions + +For details and examples, see the document [Variation Trend Calculation Functions](./Operator-and-Expression.md#variation-trend-calculation-functions). + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 9.10 Sample Functions + +For details and examples, see the document [Sample Functions](./Operator-and-Expression.md#sample-functions). + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 9.11 Change Points Function + +For details and examples, see the document [Time-Series](./Operator-and-Expression.md#change-points-function). + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 10. DATA QUALITY FUNCTION LIBRARY + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 10.1 Data Quality + +For details and examples, see the document [Data-Quality](../SQL-Manual/UDF-Libraries.md#data-quality). + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 10.2 Data Profiling + +For details and examples, see the document [Data-Profiling](../SQL-Manual/UDF-Libraries.md#data-profiling). + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 10.3 Anomaly Detection + +For details and examples, see the document [Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#anomaly-detection). + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 10.4 Frequency Domain + +For details and examples, see the document [Frequency-Domain](../SQL-Manual/UDF-Libraries.md#frequency-domain-analysis). + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 10.5 Data Matching + +For details and examples, see the document [Data-Matching](../SQL-Manual/UDF-Libraries.md#data-matching). + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 10.6 Data Repairing + +For details and examples, see the document [Data-Repairing](../SQL-Manual/UDF-Libraries.md#data-repairing). + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 10.7 Series Discovery + +For details and examples, see the document [Series-Discovery](../SQL-Manual/UDF-Libraries.md#series-discovery). + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 10.8 Machine Learning + +For details and examples, see the document [Machine-Learning](../SQL-Manual/UDF-Libraries.md#machine-learning). + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 11. LAMBDA EXPRESSION + +For details and examples, see the document [Lambda](../SQL-Manual/UDF-Libraries.md#lambda-expression). + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 12. CONDITIONAL EXPRESSION + +For details and examples, see the document [Conditional Expressions](../SQL-Manual/UDF-Libraries.md#conditional-expressions). + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +### 14.1 Configuring execution intervals + +```sql +CREATE CONTINUOUS QUERY cq1 +RESAMPLE EVERY 20s +BEGIN +SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 14.2 Configuring time range for resampling + +```sql +CREATE CONTINUOUS QUERY cq2 +RESAMPLE RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 14.3 Configuring execution intervals and CQ time ranges + +```sql +CREATE CONTINUOUS QUERY cq3 +RESAMPLE EVERY 20s RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 14.4 Configuring end_time_offset for CQ time range + +```sql +CREATE CONTINUOUS QUERY cq4 +RESAMPLE EVERY 20s RANGE 40s, 20s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 14.5 CQ without group by clause + +```sql +CREATE CONTINUOUS QUERY cq5 +RESAMPLE EVERY 20s +BEGIN + SELECT temperature + 1 + INTO root.precalculated_sg.::(temperature) + FROM root.ln.*.* + align by device +END +``` + +### 14.6 CQ Management + +#### Listing continuous queries + +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` + +#### Dropping continuous queries + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` + +#### Altering continuous queries + +CQs can't be altered once they're created. To change a CQ, you must `DROP` and re`CREATE` it with the updated settings. + +## 15. USER-DEFINED FUNCTION (UDF) + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 15.1 UDF Registration + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +### 15.2 UDF Deregistration + +```sql +DROP FUNCTION +``` + +### 15.3 UDF Queries + +```sql +SELECT example(*) from root.sg.d1 +SELECT example(s1, *) from root.sg.d1 +SELECT example(*, *) from root.sg.d1 + +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; + +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` + +### 15.4 Show All Registered UDFs + +```sql +SHOW FUNCTIONS +``` + +## 16. ADMINISTRATION MANAGEMENT + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 16.1 SQL Statements + +- Create user (Requires MANAGE_USER permission) + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- Delete user (Requires MANAGE_USER permission) + +```sql +DROP USER +eg: DROP USER user1 +``` + +- Create role (Requires MANAGE_ROLE permission) + +```sql +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- Delete role (Requires MANAGE_ROLE permission) + +```sql +DROP ROLE +eg: DROP ROLE role1 +``` + +- Grant role to user (Requires MANAGE_ROLE permission) + +```sql +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- Revoke role from user(Requires MANAGE_ROLE permission) + +```sql +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- List all user (Requires MANAGE_USER permission) + +```sql +LIST USER +``` + +- List all role (Requires MANAGE_ROLE permission) + +```sql +LIST ROLE +``` + +- List all users granted specific role.(Requires MANAGE_USER permission) + +```sql +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- List all role granted to specific user. + +```sql +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- List all privileges of user + +```sql +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; +``` + +- List all privileges of role + +```sql +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- Update password + +```sql +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 16.2 Authorization and Deauthorization + + +```sql +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +```sql +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + + +#### Delete Time Partition (experimental) + +``` +Eg: IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +#### Continuous Query,CQ + +``` +Eg: IoTDB > CREATE CONTINUOUS QUERY cq1 BEGIN SELECT max_value(temperature) INTO temperature_max FROM root.ln.*.* GROUP BY time(10s) END +``` + +#### Maintenance Command + +- FLUSH + +``` +Eg: IoTDB > flush +``` + +- MERGE + +``` +Eg: IoTDB > MERGE +Eg: IoTDB > FULL MERGE +``` + +- CLEAR CACHE + +```sql +Eg: IoTDB > CLEAR CACHE +``` + +- START REPAIR DATA + +```sql +Eg: IoTDB > START REPAIR DATA +``` + +- STOP REPAIR DATA + +```sql +Eg: IoTDB > STOP REPAIR DATA +``` + +- SET SYSTEM TO READONLY / WRITABLE + +``` +Eg: IoTDB > SET SYSTEM TO READONLY / WRITABLE +``` + +- Query abort + +``` +Eg: IoTDB > KILL QUERY 1 +``` \ No newline at end of file diff --git a/src/UserGuide/latest/SQL-Manual/SQL-Manual.md b/src/UserGuide/latest/SQL-Manual/SQL-Manual.md index 67737449a..ab81bf055 100644 --- a/src/UserGuide/latest/SQL-Manual/SQL-Manual.md +++ b/src/UserGuide/latest/SQL-Manual/SQL-Manual.md @@ -1,3 +1,6 @@ +--- +redirectTo: SQL-Manual_apache.html +--- - -# SQL Manual - -## 1. DATABASE MANAGEMENT - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -### 1.1 Create Database - -```sql -IoTDB > create database root.ln -IoTDB > create database root.sgcc -``` - -### 1.2 Show Databases - -```sql -IoTDB> SHOW DATABASES -IoTDB> SHOW DATABASES root.** -``` - -### 1.3 Delete Database - -```sql -IoTDB > DELETE DATABASE root.ln -IoTDB > DELETE DATABASE root.sgcc -// delete all data, all timeseries and all databases -IoTDB > DELETE DATABASE root.** -``` - -### 1.4 Count Databases - -```sql -IoTDB> count databases -IoTDB> count databases root.* -IoTDB> count databases root.sgcc.* -IoTDB> count databases root.sgcc -``` - -### 1.5 Setting up heterogeneous databases (Advanced operations) - -#### Set heterogeneous parameters when creating a Database - -```sql -CREATE DATABASE root.db WITH SCHEMA_REPLICATION_FACTOR=1, DATA_REPLICATION_FACTOR=3, SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; -``` - -#### Adjust heterogeneous parameters at run time - -```sql -ALTER DATABASE root.db WITH SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; -``` - -#### Show heterogeneous databases - -```sql -SHOW DATABASES DETAILS -``` - -### 1.6 TTL - -#### Set TTL - -```sql -IoTDB> set ttl to root.ln 3600000 -IoTDB> set ttl to root.sgcc.** 3600000 -IoTDB> set ttl to root.** 3600000 -``` - -#### Unset TTL - -```sql -IoTDB> unset ttl from root.ln -IoTDB> unset ttl from root.sgcc.** -IoTDB> unset ttl from root.** -``` - -#### Show TTL - -```sql -IoTDB> SHOW ALL TTL -IoTDB> SHOW TTL ON StorageGroupNames -IoTDB> SHOW DEVICES -``` - -## 2. DEVICE TEMPLATE - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -![img](/img/%E6%A8%A1%E6%9D%BF.png) - - - - - -![img](/img/templateEN.jpg) - -### 2.1 Create Device Template - -**Example 1:** Create a template containing two non-aligned timeseires - -```shell -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) -``` - -**Example 2:** Create a template containing a group of aligned timeseires - -```shell -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` - -The` lat` and `lon` measurements are aligned. - -### 2.2 Set Device Template - -```sql -IoTDB> set device template t1 to root.sg1.d1 -``` - -### 2.3 Activate Device Template - -```sql -IoTDB> set device template t1 to root.sg1.d1 -IoTDB> set device template t2 to root.sg1.d2 -IoTDB> create timeseries using device template on root.sg1.d1 -IoTDB> create timeseries using device template on root.sg1.d2 -``` - -### 2.4 Show Device Template - -```sql -IoTDB> show device templates -IoTDB> show nodes in device template t1 -IoTDB> show paths set device template t1 -IoTDB> show paths using device template t1 -``` - -### 2.5 Deactivate Device Template - -```sql -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -IoTDB> deactivate device template t1 from root.sg1.d1 -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` - -### 2.6 Unset Device Template - -```sql -IoTDB> unset device template t1 from root.sg1.d1 -``` - -### 2.7 Drop Device Template - -```sql -IoTDB> drop device template t1 -``` - -### 2.8 Alter Device Template - -```sql -IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) -``` - -## 3. TIMESERIES MANAGEMENT - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -### 3.1 Create Timeseries - -```sql -IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT -IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT -IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT -``` - -- From v0.13, you can use a simplified version of the SQL statements to create timeseries: - -```sql -IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT -IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT -IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN -IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT -``` - -- Notice that when in the CREATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below: - -```sql -IoTDB > create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN -error: encoding TS_2DIFF does not support BOOLEAN -``` - -### 3.2 Create Aligned Timeseries - -```sql -IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT , longitude FLOAT) -``` - -### 3.3 Delete Timeseries - -```sql -IoTDB> delete timeseries root.ln.wf01.wt01.status -IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware -IoTDB> delete timeseries root.ln.wf02.* -IoTDB> drop timeseries root.ln.wf02.* -``` - -### 3.4 Show Timeseries - -```sql -IoTDB> show timeseries root.** -IoTDB> show timeseries root.ln.** -IoTDB> show timeseries root.ln.** limit 10 offset 10 -IoTDB> show timeseries root.ln.** where timeseries contains 'wf01.wt' -IoTDB> show timeseries root.ln.** where dataType=FLOAT -``` - -### 3.5 Count Timeseries - -```sql -IoTDB > COUNT TIMESERIES root.** -IoTDB > COUNT TIMESERIES root.ln.** -IoTDB > COUNT TIMESERIES root.ln.*.*.status -IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status -IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' -IoTDB > COUNT TIMESERIES root.** WHERE DATATYPE = INT64 -IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' -IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' -IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 -IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1 -IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 -IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 -``` - -### 3.6 Tag and Attribute Management - -```sql -create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) -``` - -* Rename the tag/attribute key - -```SQL -ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 -``` - -* Reset the tag/attribute value - -```SQL -ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 -``` - -* Delete the existing tag/attribute - -```SQL -ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 -``` - -* Add new tags - -```SQL -ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 -``` - -* Add new attributes - -```SQL -ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 -``` - -* Upsert alias, tags and attributes - -> add alias or a new key-value if the alias or key doesn't exist, otherwise, update the old one with new value. - -```SQL -ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4) -``` - -* Show timeseries using tags. Use TAGS(tagKey) to identify the tags used as filter key - -```SQL -SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -``` - -returns all the timeseries information that satisfy the where condition and match the pathPattern. SQL statements are as follows: - -```SQL -ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c -ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 -show timeseries root.ln.** where TAGS(unit)='c' -show timeseries root.ln.** where TAGS(description) contains 'test1' -``` - -- count timeseries using tags - -```SQL -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= -``` - -returns all the number of timeseries that satisfy the where condition and match the pathPattern. SQL statements are as follows: - -```SQL -count timeseries -count timeseries root.** where TAGS(unit)='c' -count timeseries root.** where TAGS(unit)='c' group by level = 2 -``` - -create aligned timeseries - -```SQL -create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) -``` - -The execution result is as follows: - -```SQL -IoTDB> show timeseries -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| -|root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -``` - -Support query: - -```SQL -IoTDB> show timeseries where TAGS(tag1)='v1' -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| -+--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ -``` - -The above operations are supported for timeseries tag, attribute updates, etc. - -## 4. NODE MANAGEMENT - -For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). - -### 4.1 Show Child Paths - -```SQL -SHOW CHILD PATHS pathPattern -``` - -### 4.2 Show Child Nodes - -```SQL -SHOW CHILD NODES pathPattern -``` - -### 4.3 Count Nodes - -```SQL -IoTDB > COUNT NODES root.** LEVEL=2 -IoTDB > COUNT NODES root.ln.** LEVEL=2 -IoTDB > COUNT NODES root.ln.wf01.** LEVEL=3 -IoTDB > COUNT NODES root.**.temperature LEVEL=3 -``` - -### 4.4 Show Devices - -```SQL -IoTDB> show devices -IoTDB> show devices root.ln.** -IoTDB> show devices root.ln.** where device contains 't' -IoTDB> show devices with database -IoTDB> show devices root.ln.** with database -``` - -### 4.5 Count Devices - -```SQL -IoTDB> show devices -IoTDB> count devices -IoTDB> count devices root.ln.** -``` - -## 5. INSERT & LOAD DATA - -### 5.1 Insert Data - -For more details, see document [Write-Data](../Basic-Concept/Write-Data). - -#### Use of INSERT Statements - -- Insert Single Timeseries - -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) -IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1') -``` - -- Insert Multiple Timeseries - -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (2, false, 'v2') -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') -``` - -- Use the Current System Timestamp as the Timestamp of the Data Point - -```SQL -IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') -``` - -#### Insert Data Into Aligned Timeseries - -```SQL -IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) -IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(1, 1, 1) -IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(2, 2, 2), (3, 3, 3) -IoTDB > select * from root.sg1.d1 -``` - -### 5.2 Load External TsFile Tool - -For more details, see document [Data Import](../Tools-System/Data-Import-Tool.md). - -#### Load with SQL - -1. Load a single tsfile by specifying a file path (absolute path). - -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` - - -2. Load a batch of files by specifying a folder path (absolute path). - -- `load '/Users/Desktop/data'` -- `load '/Users/Desktop/data' sglevel=1` -- `load '/Users/Desktop/data' onSuccess=delete` -- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` - -#### Load with Script - -``` -./load-rewrite.bat -f D:\IoTDB\data -h 192.168.0.101 -p 6667 -u root -pw root -``` - -## 6. DELETE DATA - -For more details, see document [Write-Delete-Data](../Basic-Concept/Write-Data). - -### 6.1 Delete Single Timeseries - -```sql -IoTDB > delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; -IoTDB > delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -IoTDB > delete from root.ln.wf02.wt02.status where time < 10 -IoTDB > delete from root.ln.wf02.wt02.status where time <= 10 -IoTDB > delete from root.ln.wf02.wt02.status where time < 20 and time > 10 -IoTDB > delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 -IoTDB > delete from root.ln.wf02.wt02.status where time > 20 -IoTDB > delete from root.ln.wf02.wt02.status where time >= 20 -IoTDB > delete from root.ln.wf02.wt02.status where time = 20 -IoTDB > delete from root.ln.wf02.wt02.status where time > 4 or time < 0 -Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic -expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' -IoTDB > delete from root.ln.wf02.wt02.status -``` - -### 6.2 Delete Multiple Timeseries - -```sql -IoTDB > delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00; -IoTDB > delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; -IoTDB> delete from root.ln.wf03.wt02.status where time < now() -Msg: The statement is executed successfully. -``` - -### 6.3 Delete Time Partition (experimental) - -```sql -IoTDB > DELETE PARTITION root.ln 0,1,2 -``` - -## 7. QUERY DATA - -For more details, see document [Query-Data](../Basic-Concept/Query-Data.md). - -```sql -SELECT [LAST] selectExpr [, selectExpr] ... - [INTO intoItem [, intoItem] ...] - FROM prefixPath [, prefixPath] ... - [WHERE whereCondition] - [GROUP BY { - ([startTime, endTime), interval [, slidingStep]) | - LEVEL = levelNum [, levelNum] ... | - TAGS(tagKey [, tagKey] ... ) | - VARIATION(expression[,delta][,ignoreNull=true/false]) | - CONDITION(expression,[keep>/>=/=/ select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 -``` - -#### Select Multiple Columns of Data Based on a Time Interval - -```sql -IoTDB > select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` - -#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals - -```sql -IoTDB > select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` - -#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals - -```sql -IoTDB > select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` - -#### Order By Time Query - -```sql -IoTDB > select * from root.ln.** where time > 1 order by time desc limit 10; -``` - -### 7.2 `SELECT` CLAUSE - -#### Use Alias - -```sql -IoTDB > select s1 as temperature, s2 as speed from root.ln.wf01.wt01; -``` - -#### Nested Expressions - -##### Nested Expressions with Time Series Query - -```sql -IoTDB > select a, - b, - ((a + 1) * 2 - 1) % 2 + 1.5, - sin(a + sin(a + sin(b))), - -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 -from root.sg1; - -IoTDB > select (a + b) * 2 + sin(a) from root.sg - -IoTDB > select (a + *) / 2 from root.sg1 - -IoTDB > select (a + b) * 3 from root.sg, root.ln -``` - -##### Nested Expressions query with aggregations - -```sql -IoTDB > select avg(temperature), - sin(avg(temperature)), - avg(temperature) + 1, - -sum(hardware), - avg(temperature) + sum(hardware) -from root.ln.wf01.wt01; - -IoTDB > select avg(*), - (avg(*) + 1) * 3 / 2 -1 -from root.sg1 - -IoTDB > select avg(temperature), - sin(avg(temperature)), - avg(temperature) + 1, - -sum(hardware), - avg(temperature) + sum(hardware) as custom_sum -from root.ln.wf01.wt01 -GROUP BY([10, 90), 10ms); -``` - -#### Last Query - -```sql -IoTDB > select last status from root.ln.wf01.wt01 -IoTDB > select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 -IoTDB > select last * from root.ln.wf01.wt01 order by timeseries desc; -IoTDB > select last * from root.ln.wf01.wt01 order by dataType desc; -``` - -### 7.3 `WHERE` CLAUSE - -#### Time Filter - -```sql -IoTDB > select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; -IoTDB > select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; -IoTDB > select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` - -#### Value Filter - -```sql -IoTDB > select temperature from root.sg1.d1 where temperature > 36.5; -IoTDB > select status from root.sg1.d1 where status = true; -IoTDB > select temperature from root.sg1.d1 where temperature between 36.5 and 40; -IoTDB > select temperature from root.sg1.d1 where temperature not between 36.5 and 40; -IoTDB > select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -IoTDB > select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -IoTDB > select code from root.sg1.d1 where temperature is null; -IoTDB > select code from root.sg1.d1 where temperature is not null; -``` - -#### Fuzzy Query - -- Fuzzy matching using `Like` - -```sql -IoTDB > select * from root.sg.d1 where value like '%cc%' -IoTDB > select * from root.sg.device where value like '_b_' -``` - -- Fuzzy matching using `Regexp` - -```sql -IoTDB > select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -IoTDB > select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -``` - -### 7.4 `GROUP BY` CLAUSE - -- Aggregate By Time without Specifying the Sliding Step Length - -```sql -IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); -``` - -- Aggregate By Time Specifying the Sliding Step Length - -```sql -IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); -``` - -- Aggregate by Natural Month - -```sql -IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -``` - -- Left Open And Right Close Range - -```sql -IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); -``` - -- Aggregation By Variation - -```sql -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) -IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6+s5, 10) -``` - -- Aggregation By Condition - -```sql -IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=true) -IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=false) -``` - -- Aggregation By Session - -```sql -IoTDB > select __endTime,count(*) from root.** group by session(1d) -IoTDB > select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device -``` - -- Aggregation By Count - -```sql -IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) -IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) -``` - -- Aggregation By Level - -```sql -IoTDB > select count(status) from root.** group by level = 1 -IoTDB > select count(status) from root.** group by level = 3 -IoTDB > select count(status) from root.** group by level = 1, 3 -IoTDB > select max_value(temperature) from root.** group by level = 0 -IoTDB > select count(*) from root.ln.** group by level = 2 -``` - -- Aggregate By Time with Level Clause - -```sql -IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; -IoTDB > select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; -``` - -- Aggregation query by one single tag - -```sql -IoTDB > SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); -``` - -- Aggregation query by multiple tags - -```sql -IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); -``` - -- Downsampling Aggregation by tags based on Time Window - -```sql -IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); -``` - -### 7.5 `HAVING` CLAUSE - -Correct: - -```sql -IoTDB > select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 1 -IoTDB > select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device -``` - -Incorrect: - -```sql -IoTDB > select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 -IoTDB > select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 -IoTDB > select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 -IoTDB > select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 -``` - -### 7.6 `FILL` CLAUSE - -#### `PREVIOUS` Fill - -```sql -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); -``` - -#### `PREVIOUS` FILL and specify the fill timeout threshold -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); -``` - -#### `LINEAR` Fill - -```sql -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); -``` - -#### Constant Fill - -```sql -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); -IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); -``` - -### 7.7 `LIMIT` and `SLIMIT` CLAUSES (PAGINATION) - -#### Row Control over Query Results - -```sql -IoTDB > select status, temperature from root.ln.wf01.wt01 limit 10 -IoTDB > select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 -IoTDB > select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 2 offset 3 -IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 5 offset 3 -``` - -#### Column Control over Query Results - -```sql -IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 -IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 -IoTDB > select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 -``` - -#### Row and Column Control over Query Results - -```sql -IoTDB > select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 -``` - -### 7.8 `ORDER BY` CLAUSE - -#### Order by in ALIGN BY TIME mode - -```sql -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; -``` - -#### Order by in ALIGN BY DEVICE mode - -```sql -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -IoTDB > select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device -``` - -#### Order by arbitrary expressions - -```sql -IoTDB > select score from root.** order by score desc align by device -IoTDB > select score,total from root.one order by base+score+bonus desc -IoTDB > select score,total from root.one order by total desc -IoTDB > select base, score, bonus, total from root.** order by total desc NULLS Last, - score desc NULLS Last, - bonus desc NULLS Last, - time desc align by device -IoTDB > select min_value(total) from root.** order by min_value(total) asc align by device -IoTDB > select min_value(total),max_value(base) from root.** order by max_value(total) desc align by device -IoTDB > select score from root.** order by device asc, score desc, time asc align by device -``` - -### 7.9 `ALIGN BY` CLAUSE - -#### Align by Device - -```sql -IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -``` - -### 7.10 `INTO` CLAUSE (QUERY WRITE-BACK) - -```sql -IoTDB > select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; -IoTDB > select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); -IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -IoTDB > select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; -``` - -- Using variable placeholders: - -```sql -IoTDB > select s1, s2 -into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) -from root.sg.d1, root.sg.d2; - -IoTDB > select d1.s1, d1.s2, d2.s3, d3.s4 -into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) -from root.sg; - -IoTDB > select * into root.sg_bk.::(::) from root.sg.**; - -IoTDB > select s1, s2, s3, s4 -into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) -from root.sg.d1, root.sg.d2, root.sg.d3 -align by device; - -IoTDB > select avg(s1), sum(s2) + sum(s3), count(s4) -into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) -from root.** -align by device; - -IoTDB > select * into ::(backup_${4}) from root.sg.** align by device; - -IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -``` - -## 8. Maintennance -Generate the corresponding query plan: -``` -explain select s1,s2 from root.sg.d1 -``` -Execute the corresponding SQL, analyze the execution and output: -``` -explain analyze select s1,s2 from root.sg.d1 order by s1 -``` -## 9. OPERATOR - -For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). - -### 9.1 Arithmetic Operators - -For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-operators). - -```sql -select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 -``` - -### 9.2 Comparison Operators - -For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-operators). - -```sql -# Basic comparison operators -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; - -# `BETWEEN ... AND ...` operator -select temperature from root.sg1.d1 where temperature between 36.5 and 40; -select temperature from root.sg1.d1 where temperature not between 36.5 and 40; - -# Fuzzy matching operator: Use `Like` for fuzzy matching -select * from root.sg.d1 where value like '%cc%' -select * from root.sg.device where value like '_b_' - -# Fuzzy matching operator: Use `Regexp` for fuzzy matching -select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -select b, b like '1%', b regexp '[0-2]' from root.test; - -# `IS NULL` operator -select code from root.sg1.d1 where temperature is null; -select code from root.sg1.d1 where temperature is not null; - -# `IN` operator -select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -select a, a in (1, 2) from root.test; -``` - -### 9.3 Logical Operators - -For details and examples, see the document [Logical Operators](./Operator-and-Expression.md#logical-operators). - -```sql -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; -``` - -## 10. BUILT-IN FUNCTIONS - -For more details, see document [Operator-and-Expression](./Operator-and-Expression.md#built-in-functions). - -### 10.1 Aggregate Functions - -For details and examples, see the document [Aggregate Functions](./Operator-and-Expression.md#aggregate-functions). - -```sql -select count(status) from root.ln.wf01.wt01; - -select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; -select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; - -select time_duration(s1) from root.db.d1; -``` - -### 10.2 Arithmetic Functions - -For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-functions). - -```sql -select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; -select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; -``` - -### 10.3 Comparison Functions - -For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-functions). - -```sql -select ts, on_off(ts, 'threshold'='2') from root.test; -select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; -``` - -### 10.4 String Processing Functions - -For details and examples, see the document [String Processing](./Operator-and-Expression.md#string-processing-functions). - -```sql -select s1, string_contains(s1, 's'='warn') from root.sg1.d4; -select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; -select s1, length(s1) from root.sg1.d1 -select s1, locate(s1, "target"="1") from root.sg1.d1 -select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 -select s1, startswith(s1, "target"="1") from root.sg1.d1 -select s1, endswith(s1, "target"="1") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 -select s1, substring(s1 from 1 for 2) from root.sg1.d1 -select s1, replace(s1, 'es', 'tt') from root.sg1.d1 -select s1, upper(s1) from root.sg1.d1 -select s1, lower(s1) from root.sg1.d1 -select s3, trim(s3) from root.sg1.d1 -select s1, s2, strcmp(s1, s2) from root.sg1.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 -select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 -select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 -``` - -### 10.5 Data Type Conversion Function - -For details and examples, see the document [Data Type Conversion Function](./Operator-and-Expression.md#data-type-conversion-function). - -```sql -SELECT cast(s1 as INT32) from root.sg -``` - -### 10.6 Constant Timeseries Generating Functions - -For details and examples, see the document [Constant Timeseries Generating Functions](./Operator-and-Expression.md#constant-timeseries-generating-functions). - -```sql -select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; -``` - -### 10.7 Selector Functions - -For details and examples, see the document [Selector Functions](./Operator-and-Expression.md#selector-functions). - -```sql -select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; -``` - -### 10.8 Continuous Interval Functions - -For details and examples, see the document [Continuous Interval Functions](./Operator-and-Expression.md#continuous-interval-functions). - -```sql -select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; -``` - -### 10.9 Variation Trend Calculation Functions - -For details and examples, see the document [Variation Trend Calculation Functions](./Operator-and-Expression.md#variation-trend-calculation-functions). - -```sql -select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; - -SELECT DIFF(s1), DIFF(s2) from root.test; -SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; -``` - -### 10.10 Sample Functions - -For details and examples, see the document [Sample Functions](./Operator-and-Expression.md#sample-functions). - -```sql -select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; -select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; -select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; -select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; - -select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 -select M4(s1,'windowSize'='10') from root.vehicle.d1 -``` - -### 10.11 Change Points Function - -For details and examples, see the document [Time-Series](./Operator-and-Expression.md#change-points-function). - -```sql -select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 -``` - -## 11. DATA QUALITY FUNCTION LIBRARY - -For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). - -### 11.1 Data Quality - -For details and examples, see the document [Data-Quality](../SQL-Manual/UDF-Libraries.md#data-quality). - -```sql -# Completeness -select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Consistency -select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Timeliness -select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Validity -select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Accuracy -select Accuracy(t1,t2,t3,m1,m2,m3) from root.test -``` - -### 11.2 Data Profiling - -For details and examples, see the document [Data-Profiling](../SQL-Manual/UDF-Libraries.md#data-profiling). - -```sql -# ACF -select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 - -# Distinct -select distinct(s2) from root.test.d2 - -# Histogram -select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 - -# Integral -select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 -select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 - -# IntegralAvg -select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 - -# Mad -select mad(s0) from root.test -select mad(s0, "error"="0.01") from root.test - -# Median -select median(s0, "error"="0.01") from root.test - -# MinMax -select minmax(s1) from root.test - -# Mode -select mode(s2) from root.test.d2 - -# MvAvg -select mvavg(s1, "window"="3") from root.test - -# PACF -select pacf(s1, "lag"="5") from root.test - -# Percentile -select percentile(s0, "rank"="0.2", "error"="0.01") from root.test - -# Quantile -select quantile(s0, "rank"="0.2", "K"="800") from root.test - -# Period -select period(s1) from root.test.d3 - -# QLB -select QLB(s1) from root.test.d1 - -# Resample -select resample(s1,'every'='5m','interp'='linear') from root.test.d1 -select resample(s1,'every'='30m','aggr'='first') from root.test.d1 -select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 - -# Sample -select sample(s1,'method'='reservoir','k'='5') from root.test.d1 -select sample(s1,'method'='isometric','k'='5') from root.test.d1 - -# Segment -select segment(s1, "error"="0.1") from root.test - -# Skew -select skew(s1) from root.test.d1 - -# Spline -select spline(s1, "points"="151") from root.test - -# Spread -select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 - -# Stddev -select stddev(s1) from root.test.d1 - -# ZScore -select zscore(s1) from root.test -``` - -### 11.3 Anomaly Detection - -For details and examples, see the document [Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#anomaly-detection). - -```sql -# IQR -select iqr(s1) from root.test - -# KSigma -select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# LOF -select lof(s1,s2) from root.test.d1 where time<1000 -select lof(s1, "method"="series") from root.test.d1 where time<1000 - -# MissDetect -select missdetect(s2,'minlen'='10') from root.test.d2 - -# Range -select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# TwoSidedFilter -select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test - -# Outlier -select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test - -# MasterTrain -select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test - -# MasterDetect -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test -``` - -### 11.4 Frequency Domain - -For details and examples, see the document [Frequency-Domain](../SQL-Manual/UDF-Libraries.md#frequency-domain-analysis). - -```sql -# Conv -select conv(s1,s2) from root.test.d2 - -# Deconv -select deconv(s3,s2) from root.test.d2 -select deconv(s3,s2,'result'='remainder') from root.test.d2 - -# DWT -select dwt(s1,"method"="haar") from root.test.d1 - -# FFT -select fft(s1) from root.test.d1 -select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 - -# HighPass -select highpass(s1,'wpass'='0.45') from root.test.d1 - -# IFFT -select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 - -# LowPass -select lowpass(s1,'wpass'='0.45') from root.test.d1 - -# Envelope -select envelope(s1) from root.test.d1 -``` - -### 11.5 Data Matching - -For details and examples, see the document [Data-Matching](../SQL-Manual/UDF-Libraries.md#data-matching). - -```sql -# Cov -select cov(s1,s2) from root.test.d2 - -# DTW -select dtw(s1,s2) from root.test.d2 - -# Pearson -select pearson(s1,s2) from root.test.d2 - -# PtnSym -select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 - -# XCorr -select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 -``` - -### 11.6 Data Repairing - -For details and examples, see the document [Data-Repairing](../SQL-Manual/UDF-Libraries.md#data-repairing). - -```sql -# TimestampRepair -select timestamprepair(s1,'interval'='10000') from root.test.d2 -select timestamprepair(s1) from root.test.d2 - -# ValueFill -select valuefill(s1) from root.test.d2 -select valuefill(s1,"method"="previous") from root.test.d2 - -# ValueRepair -select valuerepair(s1) from root.test.d2 -select valuerepair(s1,'method'='LsGreedy') from root.test.d2 - -# MasterRepair -select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test - -# SeasonalRepair -select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 -select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 -``` - -### 11.7 Series Discovery - -For details and examples, see the document [Series-Discovery](../SQL-Manual/UDF-Libraries.md#series-discovery). - -```sql -# ConsecutiveSequences -select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 -select consecutivesequences(s1,s2) from root.test.d1 - -# ConsecutiveWindows -select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 -``` - -### 11.8 Machine Learning - -For details and examples, see the document [Machine-Learning](../SQL-Manual/UDF-Libraries.md#machine-learning). - -```sql -# AR -select ar(s0,"p"="2") from root.test.d0 - -# Representation -select representation(s0,"tb"="3","vb"="2") from root.test.d0 - -# RM -select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 -``` - -## 12. LAMBDA EXPRESSION - -For details and examples, see the document [Lambda](../SQL-Manual/UDF-Libraries.md#lambda-expression). - -```sql -select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` -``` - -## 13. CONDITIONAL EXPRESSION - -For details and examples, see the document [Conditional Expressions](../SQL-Manual/UDF-Libraries.md#conditional-expressions). - -```sql -select T, P, case -when 1000=1050 then "bad temperature" -when P<=1000000 or P>=1100000 then "bad pressure" -end as `result` -from root.test1 - -select str, case -when str like "%cc%" then "has cc" -when str like "%dd%" then "has dd" -else "no cc and dd" end as `result` -from root.test2 - -select -count(case when x<=1 then 1 end) as `(-∞,1]`, -count(case when 1 -[RESAMPLE - [EVERY ] - [BOUNDARY ] - [RANGE [, end_time_offset]] -] -[TIMEOUT POLICY BLOCKED|DISCARD] -BEGIN - SELECT CLAUSE - INTO CLAUSE - FROM CLAUSE - [WHERE CLAUSE] - [GROUP BY([, ]) [, level = ]] - [HAVING CLAUSE] - [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] - [LIMIT rowLimit OFFSET rowOffset] - [ALIGN BY DEVICE] -END -``` - -### 15.1 Configuring execution intervals - -```sql -CREATE CONTINUOUS QUERY cq1 -RESAMPLE EVERY 20s -BEGIN -SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) -END -``` - -### 15.2 Configuring time range for resampling - -```sql -CREATE CONTINUOUS QUERY cq2 -RESAMPLE RANGE 40s -BEGIN - SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) -END -``` - -### 15.3 Configuring execution intervals and CQ time ranges - -```sql -CREATE CONTINUOUS QUERY cq3 -RESAMPLE EVERY 20s RANGE 40s -BEGIN - SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) - FILL(100.0) -END -``` - -### 15.4 Configuring end_time_offset for CQ time range - -```sql -CREATE CONTINUOUS QUERY cq4 -RESAMPLE EVERY 20s RANGE 40s, 20s -BEGIN - SELECT max_value(temperature) - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - FROM root.ln.*.* - GROUP BY(10s) - FILL(100.0) -END -``` - -### 15.5 CQ without group by clause - -```sql -CREATE CONTINUOUS QUERY cq5 -RESAMPLE EVERY 20s -BEGIN - SELECT temperature + 1 - INTO root.precalculated_sg.::(temperature) - FROM root.ln.*.* - align by device -END -``` - -### 15.6 CQ Management - -#### Listing continuous queries - -```sql -SHOW (CONTINUOUS QUERIES | CQS) -``` - -#### Dropping continuous queries - -```sql -DROP (CONTINUOUS QUERY | CQ) -``` - -#### Altering continuous queries - -CQs can't be altered once they're created. To change a CQ, you must `DROP` and re`CREATE` it with the updated settings. - -## 16. USER-DEFINED FUNCTION (UDF) - -For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). - -### 16.1 UDF Registration - -```sql -CREATE FUNCTION AS (USING URI URI-STRING)? -``` - -### 16.2 UDF Deregistration - -```sql -DROP FUNCTION -``` - -### 16.3 UDF Queries - -```sql -SELECT example(*) from root.sg.d1 -SELECT example(s1, *) from root.sg.d1 -SELECT example(*, *) from root.sg.d1 - -SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; -SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; - -SELECT s1, s2, example(s1, s2) FROM root.sg.d1; -SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; -SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; -SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; -``` - -### 16.4 Show All Registered UDFs - -```sql -SHOW FUNCTIONS -``` - -## 17. ADMINISTRATION MANAGEMENT - -For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). - -### 17.1 SQL Statements - -- Create user (Requires MANAGE_USER permission) - -```SQL -CREATE USER -eg: CREATE USER user1 'passwd' -``` - -- Delete user (Requires MANAGE_USER permission) - -```sql -DROP USER -eg: DROP USER user1 -``` - -- Create role (Requires MANAGE_ROLE permission) - -```sql -CREATE ROLE -eg: CREATE ROLE role1 -``` - -- Delete role (Requires MANAGE_ROLE permission) - -```sql -DROP ROLE -eg: DROP ROLE role1 -``` - -- Grant role to user (Requires MANAGE_ROLE permission) - -```sql -GRANT ROLE TO -eg: GRANT ROLE admin TO user1 -``` - -- Revoke role from user(Requires MANAGE_ROLE permission) - -```sql -REVOKE ROLE FROM -eg: REVOKE ROLE admin FROM user1 -``` - -- List all user (Requires MANAGE_USER permission) - -```sql -LIST USER -``` - -- List all role (Requires MANAGE_ROLE permission) - -```sql -LIST ROLE -``` - -- List all users granted specific role.(Requires MANAGE_USER permission) - -```sql -LIST USER OF ROLE -eg: LIST USER OF ROLE roleuser -``` - -- List all role granted to specific user. - -```sql -LIST ROLE OF USER -eg: LIST ROLE OF USER tempuser -``` - -- List all privileges of user - -```sql -LIST PRIVILEGES OF USER ; -eg: LIST PRIVILEGES OF USER tempuser; -``` - -- List all privileges of role - -```sql -LIST PRIVILEGES OF ROLE ; -eg: LIST PRIVILEGES OF ROLE actor; -``` - -- Update password - -```sql -ALTER USER SET PASSWORD ; -eg: ALTER USER tempuser SET PASSWORD 'newpwd'; -``` - -### 17.2 Authorization and Deauthorization - - -```sql -GRANT ON TO ROLE/USER [WITH GRANT OPTION]; -eg: GRANT READ ON root.** TO ROLE role1; -eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; -eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; -eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; -eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; -``` - -```sql -REVOKE ON FROM ROLE/USER ; -eg: REVOKE READ ON root.** FROM ROLE role1; -eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; -eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; -eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; -eg: REVOKE ALL ON ROOT.** FROM USER user1; -``` - - -#### Delete Time Partition (experimental) - -``` -Eg: IoTDB > DELETE PARTITION root.ln 0,1,2 -``` - -#### Continuous Query,CQ - -``` -Eg: IoTDB > CREATE CONTINUOUS QUERY cq1 BEGIN SELECT max_value(temperature) INTO temperature_max FROM root.ln.*.* GROUP BY time(10s) END -``` - -#### Maintenance Command - -- FLUSH - -``` -Eg: IoTDB > flush -``` - -- MERGE - -``` -Eg: IoTDB > MERGE -Eg: IoTDB > FULL MERGE -``` - -- CLEAR CACHE - -```sql -Eg: IoTDB > CLEAR CACHE -``` - -- START REPAIR DATA - -```sql -Eg: IoTDB > START REPAIR DATA -``` - -- STOP REPAIR DATA - -```sql -Eg: IoTDB > STOP REPAIR DATA -``` - -- SET SYSTEM TO READONLY / WRITABLE - -``` -Eg: IoTDB > SET SYSTEM TO READONLY / WRITABLE -``` - -- Query abort - -``` -Eg: IoTDB > KILL QUERY 1 -``` \ No newline at end of file +--> \ No newline at end of file diff --git a/src/UserGuide/latest/SQL-Manual/SQL-Manual_apache.md b/src/UserGuide/latest/SQL-Manual/SQL-Manual_apache.md new file mode 100644 index 000000000..18ebb4f43 --- /dev/null +++ b/src/UserGuide/latest/SQL-Manual/SQL-Manual_apache.md @@ -0,0 +1,1759 @@ + + +# SQL Manual + +## 1. DATABASE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 1.1 Create Database + +```sql +IoTDB > create database root.ln +IoTDB > create database root.sgcc +``` + +### 1.2 Show Databases + +```sql +IoTDB> SHOW DATABASES +IoTDB> SHOW DATABASES root.** +``` + +### 1.3 Delete Database + +```sql +IoTDB > DELETE DATABASE root.ln +IoTDB > DELETE DATABASE root.sgcc +// delete all data, all timeseries and all databases +IoTDB > DELETE DATABASE root.** +``` + +### 1.4 Count Databases + +```sql +IoTDB> count databases +IoTDB> count databases root.* +IoTDB> count databases root.sgcc.* +IoTDB> count databases root.sgcc +``` + +### 1.5 Setting up heterogeneous databases (Advanced operations) + +#### Set heterogeneous parameters when creating a Database + +```sql +CREATE DATABASE root.db WITH SCHEMA_REPLICATION_FACTOR=1, DATA_REPLICATION_FACTOR=3, SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Adjust heterogeneous parameters at run time + +```sql +ALTER DATABASE root.db WITH SCHEMA_REGION_GROUP_NUM=1, DATA_REGION_GROUP_NUM=2; +``` + +#### Show heterogeneous databases + +```sql +SHOW DATABASES DETAILS +``` + +### 1.6 TTL + +#### Set TTL + +```sql +IoTDB> set ttl to root.ln 3600000 +IoTDB> set ttl to root.sgcc.** 3600000 +IoTDB> set ttl to root.** 3600000 +``` + +#### Unset TTL + +```sql +IoTDB> unset ttl from root.ln +IoTDB> unset ttl from root.sgcc.** +IoTDB> unset ttl from root.** +``` + +#### Show TTL + +```sql +IoTDB> SHOW ALL TTL +IoTDB> SHOW TTL ON StorageGroupNames +IoTDB> SHOW DEVICES +``` + +## 2. DEVICE TEMPLATE + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +![img](/img/%E6%A8%A1%E6%9D%BF.png) + + + + + +![img](/img/templateEN.jpg) + +### 2.1 Create Device Template + +**Example 1:** Create a template containing two non-aligned timeseires + +```shell +IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) +``` + +**Example 2:** Create a template containing a group of aligned timeseires + +```shell +IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) +``` + +The` lat` and `lon` measurements are aligned. + +### 2.2 Set Device Template + +```sql +IoTDB> set device template t1 to root.sg1.d1 +``` + +### 2.3 Activate Device Template + +```sql +IoTDB> set device template t1 to root.sg1.d1 +IoTDB> set device template t2 to root.sg1.d2 +IoTDB> create timeseries using device template on root.sg1.d1 +IoTDB> create timeseries using device template on root.sg1.d2 +``` + +### 2.4 Show Device Template + +```sql +IoTDB> show device templates +IoTDB> show nodes in device template t1 +IoTDB> show paths set device template t1 +IoTDB> show paths using device template t1 +``` + +### 2.5 Deactivate Device Template + +```sql +IoTDB> delete timeseries of device template t1 from root.sg1.d1 +IoTDB> deactivate device template t1 from root.sg1.d1 +IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* +IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* +``` + +### 2.6 Unset Device Template + +```sql +IoTDB> unset device template t1 from root.sg1.d1 +``` + +### 2.7 Drop Device Template + +```sql +IoTDB> drop device template t1 +``` + +### 2.8 Alter Device Template + +```sql +IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) +``` + +## 3. TIMESERIES MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 3.1 Create Timeseries + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- From v0.13, you can use a simplified version of the SQL statements to create timeseries: + +```sql +IoTDB > create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +IoTDB > create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +IoTDB > create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +IoTDB > create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- Notice that when in the CREATE TIMESERIES statement the encoding method conflicts with the data type, the system gives the corresponding error prompt as shown below: + +```sql +IoTDB > create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +error: encoding TS_2DIFF does not support BOOLEAN +``` + +### 3.2 Create Aligned Timeseries + +```sql +IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT , longitude FLOAT) +``` + +### 3.3 Delete Timeseries + +```sql +IoTDB> delete timeseries root.ln.wf01.wt01.status +IoTDB> delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +IoTDB> delete timeseries root.ln.wf02.* +IoTDB> drop timeseries root.ln.wf02.* +``` + +### 3.4 Show Timeseries + +```sql +IoTDB> show timeseries root.** +IoTDB> show timeseries root.ln.** +IoTDB> show timeseries root.ln.** limit 10 offset 10 +IoTDB> show timeseries root.ln.** where timeseries contains 'wf01.wt' +IoTDB> show timeseries root.ln.** where dataType=FLOAT +``` + +### 3.5 Count Timeseries + +```sql +IoTDB > COUNT TIMESERIES root.** +IoTDB > COUNT TIMESERIES root.ln.** +IoTDB > COUNT TIMESERIES root.ln.*.*.status +IoTDB > COUNT TIMESERIES root.ln.wf01.wt01.status +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +IoTDB > COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +IoTDB > COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +IoTDB > COUNT TIMESERIES root.** GROUP BY LEVEL=1 +IoTDB > COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +IoTDB > COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +### 3.6 Tag and Attribute Management + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +* Rename the tag/attribute key + +```SQL +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +* Reset the tag/attribute value + +```SQL +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +* Delete the existing tag/attribute + +```SQL +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +* Add new tags + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +* Add new attributes + +```SQL +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +* Upsert alias, tags and attributes + +> add alias or a new key-value if the alias or key doesn't exist, otherwise, update the old one with new value. + +```SQL +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag3=v3, tag4=v4) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +* Show timeseries using tags. Use TAGS(tagKey) to identify the tags used as filter key + +```SQL +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +returns all the timeseries information that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- count timeseries using tags + +```SQL +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +returns all the number of timeseries that satisfy the where condition and match the pathPattern. SQL statements are as follows: + +```SQL +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +create aligned timeseries + +```SQL +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +The execution result is as follows: + +```SQL +IoTDB> show timeseries ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| +|root.sg1.d1.s2| null| root.sg1| DOUBLE| GORILLA| SNAPPY|{"tag4":"v4","tag3":"v3"}|{"attr4":"v4","attr3":"v3"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +Support query: + +```SQL +IoTDB> show timeseries where TAGS(tag1)='v1' ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +| timeseries|alias| database|dataType|encoding|compression| tags| attributes|deadband|deadband parameters| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +|root.sg1.d1.s1| null| root.sg1| INT32| RLE| SNAPPY|{"tag1":"v1","tag2":"v2"}|{"attr2":"v2","attr1":"v1"}| null| null| ++--------------+-----+-------------+--------+--------+-----------+-------------------------+---------------------------+--------+-------------------+ +``` + +The above operations are supported for timeseries tag, attribute updates, etc. + +## 4. NODE MANAGEMENT + +For more details, see document [Operate-Metadata](../Basic-Concept/Operate-Metadata.md). + +### 4.1 Show Child Paths + +```SQL +SHOW CHILD PATHS pathPattern +``` + +### 4.2 Show Child Nodes + +```SQL +SHOW CHILD NODES pathPattern +``` + +### 4.3 Count Nodes + +```SQL +IoTDB > COUNT NODES root.** LEVEL=2 +IoTDB > COUNT NODES root.ln.** LEVEL=2 +IoTDB > COUNT NODES root.ln.wf01.** LEVEL=3 +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` + +### 4.4 Show Devices + +```SQL +IoTDB> show devices +IoTDB> show devices root.ln.** +IoTDB> show devices root.ln.** where device contains 't' +IoTDB> show devices with database +IoTDB> show devices root.ln.** with database +``` + +### 4.5 Count Devices + +```SQL +IoTDB> show devices +IoTDB> count devices +IoTDB> count devices root.ln.** +``` + +## 5. INSERT & LOAD DATA + +### 5.1 Insert Data + +For more details, see document [Write-Data](../Basic-Concept/Write-Data). + +#### Use of INSERT Statements + +- Insert Single Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1') +``` + +- Insert Multiple Timeseries + +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (2, false, 'v2') +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` + +- Use the Current System Timestamp as the Timestamp of the Data Point + +```SQL +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` + +#### Insert Data Into Aligned Timeseries + +```SQL +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(1, 1, 1) +IoTDB > insert into root.sg1.d1(time, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +IoTDB > select * from root.sg1.d1 +``` + +### 5.2 Load External TsFile Tool + +For more details, see document [Data Import](../Tools-System/Data-Import-Tool.md). + +#### Load with SQL + +1. Load a single tsfile by specifying a file path (absolute path). + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +2. Load a batch of files by specifying a folder path (absolute path). + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +#### Load with Script + +``` +./load-rewrite.bat -f D:\IoTDB\data -h 192.168.0.101 -p 6667 -u root -pw root +``` + +## 6. DELETE DATA + +For more details, see document [Write-Delete-Data](../Basic-Concept/Write-Data). + +### 6.1 Delete Single Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.status where time < 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +IoTDB > delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +IoTDB > delete from root.ln.wf02.wt02.status where time > 20 +IoTDB > delete from root.ln.wf02.wt02.status where time >= 20 +IoTDB > delete from root.ln.wf02.wt02.status where time = 20 +IoTDB > delete from root.ln.wf02.wt02.status where time > 4 or time < 0 +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +IoTDB > delete from root.ln.wf02.wt02.status +``` + +### 6.2 Delete Multiple Timeseries + +```sql +IoTDB > delete from root.ln.wf02.wt02 where time <= 2017-11-01T16:26:00; +IoTDB > delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +IoTDB> delete from root.ln.wf03.wt02.status where time < now() +Msg: The statement is executed successfully. +``` + +### 6.3 Delete Time Partition (experimental) + +```sql +IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +## 7. QUERY DATA + +For more details, see document [Query-Data](../Basic-Concept/Query-Data.md). + +```sql +SELECT [LAST] selectExpr [, selectExpr] ... + [INTO intoItem [, intoItem] ...] + FROM prefixPath [, prefixPath] ... + [WHERE whereCondition] + [GROUP BY { + ([startTime, endTime), interval [, slidingStep]) | + LEVEL = levelNum [, levelNum] ... | + TAGS(tagKey [, tagKey] ... ) | + VARIATION(expression[,delta][,ignoreNull=true/false]) | + CONDITION(expression,[keep>/>=/=/ select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` + +#### Select Multiple Columns of Data Based on a Time Interval + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Select Multiple Columns of Data for the Same Device According to Multiple Time Intervals + +```sql +IoTDB > select status,temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Choose Multiple Columns of Data for Different Devices According to Multiple Time Intervals + +```sql +IoTDB > select wf01.wt01.status,wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` + +#### Order By Time Query + +```sql +IoTDB > select * from root.ln.** where time > 1 order by time desc limit 10; +``` + +### 7.2 `SELECT` CLAUSE + +#### Use Alias + +```sql +IoTDB > select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` + +#### Nested Expressions + +##### Nested Expressions with Time Series Query + +```sql +IoTDB > select a, + b, + ((a + 1) * 2 - 1) % 2 + 1.5, + sin(a + sin(a + sin(b))), + -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 +from root.sg1; + +IoTDB > select (a + b) * 2 + sin(a) from root.sg + +IoTDB > select (a + *) / 2 from root.sg1 + +IoTDB > select (a + b) * 3 from root.sg, root.ln +``` + +##### Nested Expressions query with aggregations + +```sql +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) +from root.ln.wf01.wt01; + +IoTDB > select avg(*), + (avg(*) + 1) * 3 / 2 -1 +from root.sg1 + +IoTDB > select avg(temperature), + sin(avg(temperature)), + avg(temperature) + 1, + -sum(hardware), + avg(temperature) + sum(hardware) as custom_sum +from root.ln.wf01.wt01 +GROUP BY([10, 90), 10ms); +``` + +#### Last Query + +```sql +IoTDB > select last status from root.ln.wf01.wt01 +IoTDB > select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +IoTDB > select last * from root.ln.wf01.wt01 order by timeseries desc; +IoTDB > select last * from root.ln.wf01.wt01 order by dataType desc; +``` + +### 7.3 `WHERE` CLAUSE + +#### Time Filter + +```sql +IoTDB > select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +IoTDB > select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` + +#### Value Filter + +```sql +IoTDB > select temperature from root.sg1.d1 where temperature > 36.5; +IoTDB > select status from root.sg1.d1 where status = true; +IoTDB > select temperature from root.sg1.d1 where temperature between 36.5 and 40; +IoTDB > select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +IoTDB > select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +IoTDB > select code from root.sg1.d1 where temperature is null; +IoTDB > select code from root.sg1.d1 where temperature is not null; +``` + +#### Fuzzy Query + +- Fuzzy matching using `Like` + +```sql +IoTDB > select * from root.sg.d1 where value like '%cc%' +IoTDB > select * from root.sg.device where value like '_b_' +``` + +- Fuzzy matching using `Regexp` + +```sql +IoTDB > select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +IoTDB > select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 7.4 `GROUP BY` CLAUSE + +- Aggregate By Time without Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` + +- Aggregate By Time Specifying the Sliding Step Length + +```sql +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` + +- Aggregate by Natural Month + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +IoTDB > select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` + +- Left Open And Right Close Range + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` + +- Aggregation By Variation + +```sql +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +IoTDB > select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6+s5, 10) +``` + +- Aggregation By Condition + +```sql +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=true) +IoTDB > select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoringNull=false) +``` + +- Aggregation By Session + +```sql +IoTDB > select __endTime,count(*) from root.** group by session(1d) +IoTDB > select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` + +- Aggregation By Count + +```sql +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) +IoTDB > select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` + +- Aggregation By Level + +```sql +IoTDB > select count(status) from root.** group by level = 1 +IoTDB > select count(status) from root.** group by level = 3 +IoTDB > select count(status) from root.** group by level = 1, 3 +IoTDB > select max_value(temperature) from root.** group by level = 0 +IoTDB > select count(*) from root.ln.** group by level = 2 +``` + +- Aggregate By Time with Level Clause + +```sql +IoTDB > select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +IoTDB > select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` + +- Aggregation query by one single tag + +```sql +IoTDB > SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` + +- Aggregation query by multiple tags + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` + +- Downsampling Aggregation by tags based on Time Window + +```sql +IoTDB > SELECT avg(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` + +### 7.5 `HAVING` CLAUSE + +Correct: + +```sql +IoTDB > select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 1 +IoTDB > select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device +``` + +Incorrect: + +```sql +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 +IoTDB > select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 +IoTDB > select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` + +### 7.6 `FILL` CLAUSE + +#### `PREVIOUS` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` + +#### `PREVIOUS` FILL and specify the fill timeout threshold +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` + +#### `LINEAR` Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` + +#### Constant Fill + +```sql +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +IoTDB > select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` + +### 7.7 `LIMIT` and `SLIMIT` CLAUSES (PAGINATION) + +#### Row Control over Query Results + +```sql +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 10 +IoTDB > select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +IoTDB > select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 2 offset 3 +IoTDB > select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 5 offset 3 +``` + +#### Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +IoTDB > select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +IoTDB > select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` + +#### Row and Column Control over Query Results + +```sql +IoTDB > select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` + +### 7.8 `ORDER BY` CLAUSE + +#### Order by in ALIGN BY TIME mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` + +#### Order by in ALIGN BY DEVICE mode + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +IoTDB > select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` + +#### Order by arbitrary expressions + +```sql +IoTDB > select score from root.** order by score desc align by device +IoTDB > select score,total from root.one order by base+score+bonus desc +IoTDB > select score,total from root.one order by total desc +IoTDB > select base, score, bonus, total from root.** order by total desc NULLS Last, + score desc NULLS Last, + bonus desc NULLS Last, + time desc align by device +IoTDB > select min_value(total) from root.** order by min_value(total) asc align by device +IoTDB > select min_value(total),max_value(base) from root.** order by max_value(total) desc align by device +IoTDB > select score from root.** order by device asc, score desc, time asc align by device +``` + +### 7.9 `ALIGN BY` CLAUSE + +#### Align by Device + +```sql +IoTDB > select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` + +### 7.10 `INTO` CLAUSE (QUERY WRITE-BACK) + +```sql +IoTDB > select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +IoTDB > select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +IoTDB > select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` + +- Using variable placeholders: + +```sql +IoTDB > select s1, s2 +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) +from root.sg.d1, root.sg.d2; + +IoTDB > select d1.s1, d1.s2, d2.s3, d3.s4 +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) +from root.sg; + +IoTDB > select * into root.sg_bk.::(::) from root.sg.**; + +IoTDB > select s1, s2, s3, s4 +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) +from root.sg.d1, root.sg.d2, root.sg.d3 +align by device; + +IoTDB > select avg(s1), sum(s2) + sum(s3), count(s4) +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) +from root.** +align by device; + +IoTDB > select * into ::(backup_${4}) from root.sg.** align by device; + +IoTDB > select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` + +## 8. Maintennance +Generate the corresponding query plan: +``` +explain select s1,s2 from root.sg.d1 +``` +Execute the corresponding SQL, analyze the execution and output: +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 9. OPERATOR + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 9.1 Arithmetic Operators + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-operators). + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 9.2 Comparison Operators + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-operators). + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 9.3 Logical Operators + +For details and examples, see the document [Logical Operators](./Operator-and-Expression.md#logical-operators). + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 10. BUILT-IN FUNCTIONS + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md#built-in-functions). + +### 10.1 Aggregate Functions + +For details and examples, see the document [Aggregate Functions](./Operator-and-Expression.md#aggregate-functions). + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 10.2 Arithmetic Functions + +For details and examples, see the document [Arithmetic Operators and Functions](./Operator-and-Expression.md#arithmetic-functions). + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 10.3 Comparison Functions + +For details and examples, see the document [Comparison Operators and Functions](./Operator-and-Expression.md#comparison-functions). + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 10.4 String Processing Functions + +For details and examples, see the document [String Processing](./Operator-and-Expression.md#string-processing-functions). + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 10.5 Data Type Conversion Function + +For details and examples, see the document [Data Type Conversion Function](./Operator-and-Expression.md#data-type-conversion-function). + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 10.6 Constant Timeseries Generating Functions + +For details and examples, see the document [Constant Timeseries Generating Functions](./Operator-and-Expression.md#constant-timeseries-generating-functions). + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 10.7 Selector Functions + +For details and examples, see the document [Selector Functions](./Operator-and-Expression.md#selector-functions). + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 10.8 Continuous Interval Functions + +For details and examples, see the document [Continuous Interval Functions](./Operator-and-Expression.md#continuous-interval-functions). + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 10.9 Variation Trend Calculation Functions + +For details and examples, see the document [Variation Trend Calculation Functions](./Operator-and-Expression.md#variation-trend-calculation-functions). + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 10.10 Sample Functions + +For details and examples, see the document [Sample Functions](./Operator-and-Expression.md#sample-functions). + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 10.11 Change Points Function + +For details and examples, see the document [Time-Series](./Operator-and-Expression.md#change-points-function). + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 11. DATA QUALITY FUNCTION LIBRARY + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 11.1 Data Quality + +For details and examples, see the document [Data-Quality](../SQL-Manual/UDF-Libraries.md#data-quality). + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 11.2 Data Profiling + +For details and examples, see the document [Data-Profiling](../SQL-Manual/UDF-Libraries.md#data-profiling). + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 11.3 Anomaly Detection + +For details and examples, see the document [Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#anomaly-detection). + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 11.4 Frequency Domain + +For details and examples, see the document [Frequency-Domain](../SQL-Manual/UDF-Libraries.md#frequency-domain-analysis). + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 11.5 Data Matching + +For details and examples, see the document [Data-Matching](../SQL-Manual/UDF-Libraries.md#data-matching). + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 11.6 Data Repairing + +For details and examples, see the document [Data-Repairing](../SQL-Manual/UDF-Libraries.md#data-repairing). + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 11.7 Series Discovery + +For details and examples, see the document [Series-Discovery](../SQL-Manual/UDF-Libraries.md#series-discovery). + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 11.8 Machine Learning + +For details and examples, see the document [Machine-Learning](../SQL-Manual/UDF-Libraries.md#machine-learning). + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 12. LAMBDA EXPRESSION + +For details and examples, see the document [Lambda](../SQL-Manual/UDF-Libraries.md#lambda-expression). + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 13. CONDITIONAL EXPRESSION + +For details and examples, see the document [Conditional Expressions](../SQL-Manual/UDF-Libraries.md#conditional-expressions). + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +### 15.1 Configuring execution intervals + +```sql +CREATE CONTINUOUS QUERY cq1 +RESAMPLE EVERY 20s +BEGIN +SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 15.2 Configuring time range for resampling + +```sql +CREATE CONTINUOUS QUERY cq2 +RESAMPLE RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) +END +``` + +### 15.3 Configuring execution intervals and CQ time ranges + +```sql +CREATE CONTINUOUS QUERY cq3 +RESAMPLE EVERY 20s RANGE 40s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 15.4 Configuring end_time_offset for CQ time range + +```sql +CREATE CONTINUOUS QUERY cq4 +RESAMPLE EVERY 20s RANGE 40s, 20s +BEGIN + SELECT max_value(temperature) + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + FROM root.ln.*.* + GROUP BY(10s) + FILL(100.0) +END +``` + +### 15.5 CQ without group by clause + +```sql +CREATE CONTINUOUS QUERY cq5 +RESAMPLE EVERY 20s +BEGIN + SELECT temperature + 1 + INTO root.precalculated_sg.::(temperature) + FROM root.ln.*.* + align by device +END +``` + +### 15.6 CQ Management + +#### Listing continuous queries + +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` + +#### Dropping continuous queries + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` + +#### Altering continuous queries + +CQs can't be altered once they're created. To change a CQ, you must `DROP` and re`CREATE` it with the updated settings. + +## 16. USER-DEFINED FUNCTION (UDF) + +For more details, see document [Operator-and-Expression](../SQL-Manual/UDF-Libraries.md). + +### 16.1 UDF Registration + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +### 16.2 UDF Deregistration + +```sql +DROP FUNCTION +``` + +### 16.3 UDF Queries + +```sql +SELECT example(*) from root.sg.d1 +SELECT example(s1, *) from root.sg.d1 +SELECT example(*, *) from root.sg.d1 + +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; + +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` + +### 16.4 Show All Registered UDFs + +```sql +SHOW FUNCTIONS +``` + +## 17. ADMINISTRATION MANAGEMENT + +For more details, see document [Operator-and-Expression](./Operator-and-Expression.md). + +### 17.1 SQL Statements + +- Create user (Requires MANAGE_USER permission) + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- Delete user (Requires MANAGE_USER permission) + +```sql +DROP USER +eg: DROP USER user1 +``` + +- Create role (Requires MANAGE_ROLE permission) + +```sql +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- Delete role (Requires MANAGE_ROLE permission) + +```sql +DROP ROLE +eg: DROP ROLE role1 +``` + +- Grant role to user (Requires MANAGE_ROLE permission) + +```sql +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- Revoke role from user(Requires MANAGE_ROLE permission) + +```sql +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- List all user (Requires MANAGE_USER permission) + +```sql +LIST USER +``` + +- List all role (Requires MANAGE_ROLE permission) + +```sql +LIST ROLE +``` + +- List all users granted specific role.(Requires MANAGE_USER permission) + +```sql +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- List all role granted to specific user. + +```sql +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- List all privileges of user + +```sql +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; +``` + +- List all privileges of role + +```sql +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- Update password + +```sql +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 17.2 Authorization and Deauthorization + + +```sql +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +```sql +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + + +#### Delete Time Partition (experimental) + +``` +Eg: IoTDB > DELETE PARTITION root.ln 0,1,2 +``` + +#### Continuous Query,CQ + +``` +Eg: IoTDB > CREATE CONTINUOUS QUERY cq1 BEGIN SELECT max_value(temperature) INTO temperature_max FROM root.ln.*.* GROUP BY time(10s) END +``` + +#### Maintenance Command + +- FLUSH + +``` +Eg: IoTDB > flush +``` + +- MERGE + +``` +Eg: IoTDB > MERGE +Eg: IoTDB > FULL MERGE +``` + +- CLEAR CACHE + +```sql +Eg: IoTDB > CLEAR CACHE +``` + +- START REPAIR DATA + +```sql +Eg: IoTDB > START REPAIR DATA +``` + +- STOP REPAIR DATA + +```sql +Eg: IoTDB > STOP REPAIR DATA +``` + +- SET SYSTEM TO READONLY / WRITABLE + +``` +Eg: IoTDB > SET SYSTEM TO READONLY / WRITABLE +``` + +- Query abort + +``` +Eg: IoTDB > KILL QUERY 1 +``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md b/src/zh/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md index f20acc23c..084a972bd 100644 --- a/src/zh/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md +++ b/src/zh/UserGuide/Master/Tree/Basic-Concept/Operate-Metadata_timecho.md @@ -339,239 +339,10 @@ It costs 0.058s + 数据库允许拥有的最大 DataRegionGroup 数量 -## 2. 设备模板管理 -IoTDB 支持设备模板功能,实现同类型不同实体的物理量元数据共享,减少元数据内存占用,同时简化同类型实体的管理。 +## 2. 时间序列管理 - -![img](/img/%E6%A8%A1%E6%9D%BF.png) - -![img](/img/template.jpg) - -### 2.1 创建设备模板 - -创建设备模板的 SQL 语法如下: - -```sql -CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' -``` - -**示例1:** 创建包含两个非对齐序列的元数据模板 - -```shell -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN ) -``` - -**示例2:** 创建包含一组对齐序列的元数据模板 - -```shell -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` - -其中,物理量 `lat` 和 `lon` 是对齐的。 - -创建模板时,系统会默认指定编码压缩方式,无需手动指定,若业务场景需要手动调整,可参考如下示例: -```shell -IoTDB> create device template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY) -``` -更多详细的数据类型与编码方式的对应列表请参见 [压缩&编码](../Technical-Insider/Encoding-and-Compression.md)。 - -### 2.2 挂载设备模板 - -元数据模板在创建后,需执行挂载操作,方可用于相应路径下的序列创建与数据写入。 - -**挂载模板前,需确保相关数据库已经创建。** - -**推荐将模板挂载在 database 节点上,不建议将模板挂载到 database 上层的节点上。** - -**模板挂载路径下禁止创建普通序列,已创建了普通序列的前缀路径上不允许挂载模板。** - -挂载元数据模板的 SQL 语句如下所示: - -```shell -IoTDB> set device template t1 to root.sg1.d1 -``` - -### 2.3 激活设备模板 - -挂载好设备模板后,且系统开启自动注册序列功能的情况下,即可直接进行数据的写入。例如 database 为 root.sg1,模板 t1 被挂载到了节点 root.sg1.d1,那么可直接向时间序列(如 root.sg1.d1.temperature 和 root.sg1.d1.status)写入时间序列数据,该时间序列已可被当作正常创建的序列使用。 - -**注意**:在插入数据之前或系统未开启自动注册序列功能,模板定义的时间序列不会被创建。可以使用如下SQL语句在插入数据前创建时间序列即激活模板: - -```shell -IoTDB> create timeseries using device template on root.sg1.d1 -``` - -**示例:** 执行以下语句 -```shell -IoTDB> set device template t1 to root.sg1.d1 -IoTDB> set device template t2 to root.sg1.d2 -IoTDB> create timeseries using device template on root.sg1.d1 -IoTDB> create timeseries using device template on root.sg1.d2 -``` - -查看此时的时间序列: -```sql -show timeseries root.sg1.** -``` - -```shell -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression|tags|attributes|deadband|deadband parameters| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -|root.sg1.d1.temperature| null| root.sg1| FLOAT| RLE| SNAPPY|null| null| null| null| -| root.sg1.d1.status| null| root.sg1| BOOLEAN| PLAIN| SNAPPY|null| null| null| null| -| root.sg1.d2.lon| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -| root.sg1.d2.lat| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -``` - -查看此时的设备: -```sql -show devices root.sg1.** -``` - -```shell -+---------------+---------+---------+ -| devices|isAligned| Template| -+---------------+---------+---------+ -| root.sg1.d1| false| null| -| root.sg1.d2| true| null| -+---------------+---------+---------+ -``` - -### 2.4 查看设备模板 - -- 查看所有设备模板 - -SQL 语句如下所示: - -```shell -IoTDB> show device templates -``` - -执行结果如下: -```shell -+-------------+ -|template name| -+-------------+ -| t2| -| t1| -+-------------+ -``` - -- 查看某个设备模板下的物理量 - -SQL 语句如下所示: - -```shell -IoTDB> show nodes in device template t1 -``` - -执行结果如下: -```shell -+-----------+--------+--------+-----------+ -|child nodes|dataType|encoding|compression| -+-----------+--------+--------+-----------+ -|temperature| FLOAT| RLE| SNAPPY| -| status| BOOLEAN| PLAIN| SNAPPY| -+-----------+--------+--------+-----------+ -``` - -- 查看挂载了某个设备模板的路径 - -```shell -IoTDB> show paths set device template t1 -``` - -执行结果如下: -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -- 查看使用了某个设备模板的路径(即模板在该路径上已激活,序列已创建) - -```shell -IoTDB> show paths using device template t1 -``` - -执行结果如下: -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -### 2.5 解除设备模板 - -若需删除模板表示的某一组时间序列,可采用解除模板操作,SQL语句如下所示: - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -``` - -或 - -```shell -IoTDB> deactivate device template t1 from root.sg1.d1 -``` - -解除操作支持批量处理,SQL语句如下所示: - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -``` - -或 - -```shell -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` - -若解除命令不指定模板名称,则会将给定路径涉及的所有模板使用情况均解除。 - -### 2.6 卸载设备模板 - -卸载设备模板的 SQL 语句如下所示: - -```shell -IoTDB> unset device template t1 from root.sg1.d1 -``` - -**注意**:不支持卸载仍处于激活状态的模板,需保证执行卸载操作前解除对该模板的所有使用,即删除所有该模板表示的序列。 - -### 2.7 删除设备模板 - -删除设备模板的 SQL 语句如下所示: - -```shell -IoTDB> drop device template t1 -``` - -**注意**:不支持删除已经挂载的模板,需在删除操作前保证该模板卸载成功。 - -### 2.8 修改设备模板 - -在需要新增物理量的场景中,可以通过修改设备模板来给所有已激活该模板的设备新增物理量。 - -修改设备模板的 SQL 语句如下所示: - -```shell -IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) -``` - -**向已挂载模板的路径下的设备中写入数据,若写入请求中的物理量不在模板中,将自动扩展模板。** - - -## 3. 时间序列管理 - -### 3.1 创建时间序列 +### 2.1 创建时间序列 根据建立的数据模型,我们可以分别在两个数据库中创建相应的时间序列。创建时间序列的 SQL 语句如下所示: @@ -609,7 +380,7 @@ error: encoding TS_2DIFF does not support BOOLEAN 更多详细的数据类型与编码压缩方式的对应列表请参见 [压缩&编码](../Technical-Insider/Encoding-and-Compression.md)。 -### 3.2 创建对齐时间序列 +### 2.2 创建对齐时间序列 创建一组对齐时间序列的SQL语句如下所示: @@ -621,7 +392,7 @@ IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOA 对齐的时间序列也支持设置别名、标签、属性。 -### 3.3 删除时间序列 +### 2.3 删除时间序列 我们可以使用`(DELETE | DROP) TimeSeries `语句来删除我们之前创建的时间序列。SQL 语句如下所示: @@ -632,7 +403,7 @@ IoTDB> delete timeseries root.ln.wf02.* IoTDB> drop timeseries root.ln.wf02.* ``` -### 3.4 查看时间序列 +### 2.4 查看时间序列 * SHOW LATEST? TIMESERIES pathPattern? timeseriesWhereClause? limitClause? @@ -776,7 +547,7 @@ It costs 0.004s 需要注意的是,当查询路径不存在时,系统会返回 0 条时间序列。 -### 3.5 统计时间序列总数 +### 2.5 统计时间序列总数 IoTDB 支持使用`COUNT TIMESERIES`来统计一条路径中的时间序列个数。SQL 语句如下所示: @@ -863,7 +634,7 @@ It costs 0.002s > 注意:时间序列的路径只是过滤条件,与 level 的定义无关。 -### 3.6 活跃时间序列查询 +### 2.6 活跃时间序列查询 我们在原有的时间序列查询和统计上添加新的WHERE时间过滤条件,可以得到在指定时间范围中存在数据的时间序列。 需要注意的是, 在带有时间过滤的元数据查询中并不考虑视图的存在,只考虑TsFile中实际存储的时间序列。 @@ -904,7 +675,7 @@ IoTDB> count timeseries where time >= 15000 and time < 16000; ``` 关于活跃时间序列的定义,能通过正常查询查出来的数据就是活跃数据,也就是说插入但被删除的时间序列不在考虑范围内。 -### 3.7 标签点管理 +### 2.7 标签点管理 我们可以在创建时间序列的时候,为它添加别名和额外的标签和属性信息。 @@ -1067,9 +838,9 @@ IoTDB> show timeseries where TAGS(tag1)='v1' 上述对时间序列标签、属性的更新等操作都支持。 -## 4. 路径查询 +## 3. 路径查询 -### 4.1 路径(Path) +### 3.1 路径(Path) 路径(path)是用于表示时间序列的层级结构的表达式,其语法定义如下: ```SQL @@ -1087,7 +858,7 @@ IoTDB> show timeseries where TAGS(tag1)='v1' | '**' ; ``` -### 4.2 路径结点名(NodeName) +### 3.2 路径结点名(NodeName) - 路径中由 `.` 分割的部分称为路径结点名(nodeName)。 - 例如,`root.a.b.c` 是一个层级为 4 的路径,其中 root、a、b 和 c 都是路径结点名。 @@ -1102,11 +873,11 @@ IoTDB> show timeseries where TAGS(tag1)='v1' - UNICODE 中文字符(\u2E80 到 \u9FFF) - 大小写敏感性:在 Windows 系统上,数据库路径结点名是大小写不敏感的。例如,root.ln 和 root.LN 会被视为相同的路径。 -### 4.3 特殊字符(反引号) +### 3.3 特殊字符(反引号) 如果`路径结点名(NodeName)`中需要使用特殊字符(如空格、标点符号等),可以使用反引号(`)将结点名引用起来。更多关于反引号的使用方法,请参考[反引号](../SQL-Manual/Syntax-Rule.md#反引号)。 -### 4.4 路径模式(Path Pattern) +### 3.4 路径模式(Path Pattern) 为了使得在表达多个时间序列的时候更加方便快捷,IoTDB 为用户提供带通配符`*`或`**`的路径。通配符可以出现在路径中的任何层。 @@ -1119,7 +890,7 @@ IoTDB> show timeseries where TAGS(tag1)='v1' **注意**:* 和 ** 不能放在路径的开头。 -### 4.5 查看路径的所有子路径 +### 3.5 查看路径的所有子路径 ``` SHOW CHILD PATHS pathPattern @@ -1155,7 +926,7 @@ It costs 0.002s +---------------+ ``` -### 4.6 查看路径的下一级节点 +### 3.6 查看路径的下一级节点 ``` SHOW CHILD NODES pathPattern @@ -1186,7 +957,7 @@ SHOW CHILD NODES pathPattern +------------+ ``` -### 4.7 统计节点数 +### 3.7 统计节点数 IoTDB 支持使用`COUNT NODES LEVEL=`来统计当前 Metadata 树下满足某路径模式的路径中指定层级的节点个数。这条语句可以用来统计带有特定采样点的设备数。例如: @@ -1236,7 +1007,7 @@ It costs 0.002s > 注意:时间序列的路径只是过滤条件,与 level 的定义无关。 -### 4.8 查看设备 +### 3.8 查看设备 * SHOW DEVICES pathPattern? (WITH DATABASE)? devicesWhereClause? limitClause? @@ -1348,7 +1119,7 @@ Total line number = 2 It costs 0.001s ``` -### 4.9 统计设备数量 +### 3.9 统计设备数量 * COUNT DEVICES \ @@ -1393,7 +1164,7 @@ Total line number = 1 It costs 0.004s ``` -### 4.10 活跃设备查询 +### 3.10 活跃设备查询 和活跃时间序列一样,我们可以在查看和统计设备的基础上添加时间过滤条件来查询在某段时间内存在数据的活跃设备。这里活跃的定义与活跃时间序列相同,使用样例如下: ``` IoTDB> insert into root.sg.data(timestamp, s1,s2) values(15000, 1, 2); diff --git a/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md b/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md index 1b35aacd8..ab81bf055 100644 --- a/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md +++ b/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual.md @@ -1,1973 +1,23 @@ -# SQL手册 - -## 1. 元数据操作 - -### 1.1 数据库管理 - -#### 创建数据库 - -```sql -CREATE DATABASE root.ln -``` - -#### 查看数据库 - -```sql -show databases -show databases root.* -show databases root.** -``` - -#### 删除数据库 - -```sql -DELETE DATABASE root.ln -DELETE DATABASE root.sgcc -DELETE DATABASE root.** -``` - -#### 统计数据库数量 - -```sql -count databases -count databases root.* -count databases root.sgcc.* -count databases root.sgcc -``` - -### 1.2 时间序列管理 - -#### 创建时间序列 - -```sql -create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN -create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT -create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT -create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN -create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN -create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT -``` - -- 简化版 - -```sql -create timeseries root.ln.wf01.wt01.status BOOLEAN -create timeseries root.ln.wf01.wt01.temperature FLOAT -create timeseries root.ln.wf02.wt02.hardware TEXT -create timeseries root.ln.wf02.wt02.status BOOLEAN -create timeseries root.sgcc.wf03.wt01.status BOOLEAN -create timeseries root.sgcc.wf03.wt01.temperature FLOAT -``` - -- 错误提示 - -```sql -create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN -> error: encoding TS_2DIFF does not support BOOLEAN -``` - -#### 创建对齐时间序列 - -```sql -CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOAT) -``` - -#### 删除时间序列 - -```sql -delete timeseries root.ln.wf01.wt01.status -delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware -delete timeseries root.ln.wf02.* -drop timeseries root.ln.wf02.* -``` - -#### 查看时间序列 - -```sql -SHOW TIMESERIES -SHOW TIMESERIES -SHOW TIMESERIES root.** -SHOW TIMESERIES root.ln.** -SHOW TIMESERIES root.ln.** limit 10 offset 10 -SHOW TIMESERIES root.ln.** where timeseries contains 'wf01.wt' -SHOW TIMESERIES root.ln.** where dataType=FLOAT -SHOW TIMESERIES root.ln.** where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -SHOW LATEST TIMESERIES -``` - -#### 统计时间序列数量 - -```sql -COUNT TIMESERIES root.** -COUNT TIMESERIES root.ln.** -COUNT TIMESERIES root.ln.*.*.status -COUNT TIMESERIES root.ln.wf01.wt01.status -COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' -COUNT TIMESERIES root.** WHERE DATATYPE = INT64 -COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' -COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' -COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 -COUNT TIMESERIES root.** WHERE time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -COUNT TIMESERIES root.** GROUP BY LEVEL=1 -COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 -COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 -``` - -#### 标签点管理 - -```sql -create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) -``` - -- 重命名标签或属性 - -```sql -ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 -``` - -- 重新设置标签或属性的值 - -```sql -ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 -``` - -- 删除已经存在的标签或属性 - -```sql -ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 -``` - -- 添加新的标签 - -```sql -ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 -``` - -- 添加新的属性 - -```sql -ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 -``` - -- 更新插入别名,标签和属性 - -```sql -ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4) -``` - -- 使用标签作为过滤条件查询时间序列 - -```sql -SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -``` - -返回给定路径的下的所有满足条件的时间序列信息: - -```sql -ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c -ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 -show timeseries root.ln.** where TAGS(unit)='c' -show timeseries root.ln.** where TAGS(description) contains 'test1' -``` - -- 使用标签作为过滤条件统计时间序列数量 - -```sql -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= -``` - -返回给定路径的下的所有满足条件的时间序列的数量: - -```sql -count timeseries -count timeseries root.** where TAGS(unit)='c' -count timeseries root.** where TAGS(unit)='c' group by level = 2 -``` - -创建对齐时间序列: - -```sql -create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) -``` - -支持查询: - -```sql -show timeseries where TAGS(tag1)='v1' -``` - -### 1.3 时间序列路径管理 - -#### 查看路径的所有子路径 - -```sql -SHOW CHILD PATHS pathPattern -- 查询 root.ln 的下一层:show child paths root.ln -- 查询形如 root.xx.xx.xx 的路径:show child paths root.*.* -``` -#### 查看路径的所有子节点 - -```sql -SHOW CHILD NODES pathPattern - -- 查询 root 的下一层:show child nodes root -- 查询 root.ln 的下一层 :show child nodes root.ln -``` -#### 查看设备 - -```sql -IoTDB> show devices - -IoTDB> show devices root.ln.** - -IoTDB> show devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -``` -##### 查看设备及其 database 信息 - -```sql -IoTDB> show devices with database - -IoTDB> show devices root.ln.** with database -``` -#### 统计节点数 - -```sql -IoTDB > COUNT NODES root.** LEVEL=2 - -IoTDB > COUNT NODES root.ln.** LEVEL=2 - -IoTDB > COUNT NODES root.ln.wf01.* LEVEL=3 - -IoTDB > COUNT NODES root.**.temperature LEVEL=3 -``` -#### 统计设备数量 - -```sql - -IoTDB> count devices - -IoTDB> count devices root.ln.** - -IoTDB> count devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -``` -### 1.4 设备模板管理 - - -![img](/img/%E6%A8%A1%E6%9D%BF.png) -![img](/img/template.jpg) - - - -#### 创建设备模板 - -```Go -CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' -``` - -创建包含两个非对齐序列的设备模板 -```sql -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) -``` -创建包含一组对齐序列的设备模板 -```sql -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` -#### 挂载设备模板 -```sql -IoTDB> set DEVICE TEMPLATE t1 to root.sg1 -``` -#### 激活设备模板 -```sql -IoTDB> create timeseries using DEVICE TEMPLATE on root.sg1.d1 - -IoTDB> set DEVICE TEMPLATE t1 to root.sg1.d1 - -IoTDB> set DEVICE TEMPLATE t2 to root.sg1.d2 - -IoTDB> create timeseries using device template on root.sg1.d1 - -IoTDB> create timeseries using device template on root.sg1.d2 -``` -#### 查看设备模板 -```sql -IoTDB> show device templates -``` -- 查看某个设备模板下的物理量 -```sql -IoTDB> show nodes in device template t1 -``` -- 查看挂载了某个设备模板的路径 -```sql -IoTDB> show paths set device template t1 -``` -- 查看使用了某个设备模板的路径(即模板在该路径上已激活,序列已创建) -```sql -IoTDB> show paths using device template t1 -``` -#### 解除设备模板 -```sql -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -``` -```sql -IoTDB> deactivate device template t1 from root.sg1.d1 -``` -批量处理 -```sql -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -``` -```sql -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` -#### 卸载设备模板 -```sql -IoTDB> unset device template t1 from root.sg1.d1 -``` -#### 删除设备模板 -```sql -IoTDB> drop device template t1 -``` -### 1.5 数据存活时间管理 - -#### 设置 TTL -```sql -IoTDB> set ttl to root.ln 3600000 -``` -```sql -IoTDB> set ttl to root.sgcc.** 3600000 -``` -```sql -IoTDB> set ttl to root.** 3600000 -``` -#### 取消 TTL -```sql -IoTDB> unset ttl from root.ln -``` -```sql -IoTDB> unset ttl from root.sgcc.** -``` -```sql -IoTDB> unset ttl from root.** -``` - -#### 显示 TTL -```sql -IoTDB> SHOW ALL TTL -``` -```sql -IoTDB> SHOW TTL ON pathPattern -``` -```sql -IoTDB> show DEVICES -``` -## 2. 写入数据 - -### 2.1 写入单列数据 -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) -``` -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1'),(2, 'v1') -``` -### 2.2 写入多列数据 -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) values (2, false, 'v2') -``` -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') -``` -### 2.3 使用服务器时间戳 -```sql -IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') -``` -### 2.4 写入对齐时间序列数据 -```sql -IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) -``` -```sql -IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(1, 1, 1) -``` -```sql -IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(2, 2, 2), (3, 3, 3) -``` -```sql -IoTDB > select * from root.sg1.d1 -``` -### 2.5 加载 TsFile 文件数据 - -load '' [sglevel=int][onSuccess=delete/none] - -#### 通过指定文件路径(绝对路径)加载单 tsfile 文件 - -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` - - -#### 通过指定文件夹路径(绝对路径)批量加载文件 - -- `load '/Users/Desktop/data'` -- `load '/Users/Desktop/data' sglevel=1` -- `load '/Users/Desktop/data' onSuccess=delete` -- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` - -## 3. 删除数据 - -### 3.1 删除单列数据 -```sql -delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; -``` -```sql -delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -``` -```sql -delete from root.ln.wf02.wt02.status where time < 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time <= 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time < 20 and time > 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time > 20 -``` -```sql -delete from root.ln.wf02.wt02.status where time >= 20 -``` -```sql -delete from root.ln.wf02.wt02.status where time = 20 -``` -出错: -```sql -delete from root.ln.wf02.wt02.status where time > 4 or time < 0 - -Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic - -expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' -``` - -删除时间序列中的所有数据: -```sql -delete from root.ln.wf02.wt02.status -``` -### 3.2 删除多列数据 -```sql -delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; -``` -声明式的编程方式: -```sql -IoTDB> delete from root.ln.wf03.wt02.status where time < now() - -Msg: The statement is executed successfully. -``` -## 4. 数据查询 - -### 4.1 基础查询 - -#### 时间过滤查询 -```sql -select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 -``` -#### 根据一个时间区间选择多列数据 -```sql -select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` -#### 按照多个时间区间选择同一设备的多列数据 -```sql -select status, temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` -#### 按照多个时间区间选择不同设备的多列数据 -```sql -select wf01.wt01.status, wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` -#### 根据时间降序返回结果集 -```sql -select * from root.ln.** where time > 1 order by time desc limit 10; -``` -### 4.2 选择表达式 - -#### 使用别名 -```sql -select s1 as temperature, s2 as speed from root.ln.wf01.wt01; -``` -#### 运算符 - -#### 函数 - -不支持: -```sql -select s1, count(s1) from root.sg.d1; - -select sin(s1), count(s1) from root.sg.d1; - -select s1, count(s1) from root.sg.d1 group by ([10,100),10ms); -``` -##### 时间序列查询嵌套表达式 - -示例 1: -```sql -select a, - -​ b, - -​ ((a + 1) * 2 - 1) % 2 + 1.5, - -​ sin(a + sin(a + sin(b))), - -​ -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 - -from root.sg1; -``` -示例 2: -```sql -select (a + b) * 2 + sin(a) from root.sg - -示例 3: - -select (a + *) / 2 from root.sg1 - -示例 4: - -select (a + b) * 3 from root.sg, root.ln -``` -##### 聚合查询嵌套表达式 - -示例 1: -```sql -select avg(temperature), - -​ sin(avg(temperature)), - -​ avg(temperature) + 1, - -​ -sum(hardware), - -​ avg(temperature) + sum(hardware) - -from root.ln.wf01.wt01; -``` -示例 2: -```sql -select avg(*), - -​ (avg(*) + 1) * 3 / 2 -1 - -from root.sg1 -``` -示例 3: -```sql -select avg(temperature), - -​ sin(avg(temperature)), - -​ avg(temperature) + 1, - -​ -sum(hardware), - -​ avg(temperature) + sum(hardware) as custom_sum - -from root.ln.wf01.wt01 - -GROUP BY([10, 90), 10ms); -``` -#### 最新点查询 - -SQL 语法: - -```Go -select last [COMMA ]* from < PrefixPath > [COMMA < PrefixPath >]* [ORDER BY TIMESERIES (DESC | ASC)?] -``` - -查询 root.ln.wf01.wt01.status 的最新数据点 -```sql -IoTDB> select last status from root.ln.wf01.wt01 -``` -查询 root.ln.wf01.wt01 下 status,temperature 时间戳大于等于 2017-11-07T23:50:00 的最新数据点 -```sql -IoTDB> select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 -``` - 查询 root.ln.wf01.wt01 下所有序列的最新数据点,并按照序列名降序排列 -```sql -IoTDB> select last * from root.ln.wf01.wt01 order by timeseries desc; -``` -### 4.3 查询过滤条件 - -#### 时间过滤条件 - -选择时间戳大于 2022-01-01T00:05:00.000 的数据: -```sql -select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; -``` -选择时间戳等于 2022-01-01T00:05:00.000 的数据: -```sql -select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; -``` -选择时间区间 [2017-11-01T00:05:00.000, 2017-11-01T00:12:00.000) 内的数据: -```sql -select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` -#### 值过滤条件 - -选择值大于 36.5 的数据: -```sql -select temperature from root.sg1.d1 where temperature > 36.5; -``` -选择值等于 true 的数据: -```sql -select status from root.sg1.d1 where status = true; -``` -选择区间 [36.5,40] 内或之外的数据: -```sql -select temperature from root.sg1.d1 where temperature between 36.5 and 40; -``` -```sql -select temperature from root.sg1.d1 where temperature not between 36.5 and 40; -``` -选择值在特定范围内的数据: -```sql -select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -``` -选择值在特定范围外的数据: -```sql -select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -``` -选择值为空的数据: -```sql -select code from root.sg1.d1 where temperature is null; -``` -选择值为非空的数据: -```sql -select code from root.sg1.d1 where temperature is not null; -``` -#### 模糊查询 - -查询 `root.sg.d1` 下 `value` 含有`'cc'`的数据 -```sql -IoTDB> select * from root.sg.d1 where value like '%cc%' -``` -查询 `root.sg.d1` 下 `value` 中间为 `'b'`、前后为任意单个字符的数据 -```sql -IoTDB> select * from root.sg.device where value like '_b_' -``` -查询 root.sg.d1 下 value 值为26个英文字符组成的字符串 -```sql -IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -``` - -查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的 -```sql -IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -``` - -### 4.4 分段分组聚合 - -#### 未指定滑动步长的时间区间分组聚合查询 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); -``` -#### 指定滑动步长的时间区间分组聚合查询 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); -``` -滑动步长可以小于聚合窗口 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-01 10:00:00), 4h, 2h); -``` -#### 按照自然月份的时间区间分组聚合查询 -```sql -select count(status) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -``` -每个时间间隔窗口内都有数据 -```sql -select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -``` -#### 左开右闭区间 -```sql -select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); -``` -#### 与分组聚合混合使用 - -统计降采样后的数据点个数 -```sql -select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; -``` -加上滑动 Step 的降采样后的结果也可以汇总 -```sql -select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; -``` -#### 路径层级分组聚合 - -统计不同 database 下 status 序列的数据点个数 -```sql -select count(status) from root.** group by level = 1 -``` - 统计不同设备下 status 序列的数据点个数 -```sql -select count(status) from root.** group by level = 3 -``` -统计不同 database 下的不同设备中 status 序列的数据点个数 -```sql -select count(status) from root.** group by level = 1, 3 -``` -查询所有序列下温度传感器 temperature 的最大值 -```sql -select max_value(temperature) from root.** group by level = 0 -``` -查询某一层级下所有传感器拥有的总数据点数 -```sql -select count(*) from root.ln.** group by level = 2 -``` -#### 标签分组聚合 - -##### 单标签聚合查询 -```sql -SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); -``` -##### 多标签聚合查询 -```sql -SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); -``` -##### 基于时间区间的标签聚合查询 -```sql -SELECT AVG(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); -``` -#### 差值分段聚合 -```sql -group by variation(controlExpression[,delta][,ignoreNull=true/false]) -``` -##### delta=0时的等值事件分段 -```sql -select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) -``` -指定ignoreNull为false -```sql -select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) -``` -##### delta!=0时的差值事件分段 -```sql -select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) -``` -#### 条件分段聚合 -```sql -group by condition(predict,[keep>/>=/=/<=/<]threshold,[,ignoreNull=true/false]) -``` -查询至少连续两行以上的charging_status=1的数据 -```sql -select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=true) -``` -当设置`ignoreNull`为false时,遇到null值为将其视为一个不满足条件的行,得到结果原先的分组被含null的行拆分 -```sql -select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=false) -``` -#### 会话分段聚合 -```sql -group by session(timeInterval) -``` -按照不同的时间单位设定时间间隔 -```sql -select __endTime,count(*) from root.** group by session(1d) -``` -和`HAVING`、`ALIGN BY DEVICE`共同使用 -```sql -select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device -``` -#### 点数分段聚合 -```sql -group by count(controlExpression, size[,ignoreNull=true/false]) -``` -select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) - -当使用ignoreNull将null值也考虑进来 -```sql -select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) -``` -### 4.5 聚合结果过滤 - -不正确的: -```sql -select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 - -select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 - -select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 - -select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 -``` -SQL 示例: -```sql - select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 2; - - select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device; -``` -### 4.6 结果集补空值 -```sql -FILL '(' PREVIOUS | LINEAR | constant (, interval=DURATION_LITERAL)? ')' -``` -#### `PREVIOUS` 填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); -``` -#### `PREVIOUS` 填充并指定填充超时阈值 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); -``` -#### `LINEAR` 填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); -``` -#### 常量填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); -``` -使用 `BOOLEAN` 类型的常量填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); -``` -### 4.7 查询结果分页 - -#### 按行分页 - - 基本的 `LIMIT` 子句 -```sql -select status, temperature from root.ln.wf01.wt01 limit 10 -``` -带 `OFFSET` 的 `LIMIT` 子句 -```sql -select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 -``` -`LIMIT` 子句与 `WHERE` 子句结合 -```sql -select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 -``` - `LIMIT` 子句与 `GROUP BY` 子句组合 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 -``` -#### 按列分页 - - 基本的 `SLIMIT` 子句 -```sql -select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 -``` -带 `SOFFSET` 的 `SLIMIT` 子句 -```sql -select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 -``` -`SLIMIT` 子句与 `GROUP BY` 子句结合 -```sql -select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 -``` -`SLIMIT` 子句与 `LIMIT` 子句结合 -```sql -select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 -``` -### 4.8 排序 - -时间对齐模式下的排序 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; -``` -设备对齐模式下的排序 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; -``` -在时间戳相等时按照设备名排序 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; -``` -没有显式指定时 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -``` -对聚合后的结果进行排序 -```sql -select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device -``` -### 4.9 查询对齐模式 - -#### 按设备对齐 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -``` -### 4.10 查询写回(SELECT INTO) - -#### 整体描述 -```sql -selectIntoStatement - -​ : SELECT - -​ resultColumn [, resultColumn] ... - -​ INTO intoItem [, intoItem] ... - -​ FROM prefixPath [, prefixPath] ... - -​ [WHERE whereCondition] - -​ [GROUP BY groupByTimeClause, groupByLevelClause] - -​ [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] - -​ [LIMIT rowLimit OFFSET rowOffset] - -​ [ALIGN BY DEVICE] - -​ ; - - - -intoItem - -​ : [ALIGNED] intoDevicePath '(' intoMeasurementName [',' intoMeasurementName]* ')' - -​ ; -``` -按时间对齐,将 `root.sg` database 下四条序列的查询结果写入到 `root.sg_copy` database 下指定的四条序列中 -```sql -IoTDB> select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; -``` -按时间对齐,将聚合查询的结果存储到指定序列中 -```sql -IoTDB> select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); -``` -按设备对齐 -```sql -IoTDB> select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -``` -按设备对齐,将表达式计算的结果存储到指定序列中 -```sql -IoTDB> select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; -``` -#### 使用变量占位符 - -##### 按时间对齐(默认) - -###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 -``` - -select s1, s2 - -into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) - -from root.sg.d1, root.sg.d2; -``` - -该语句等价于: -``` - -select s1, s2 - -into root.sg_copy.d1(s1), root.sg_copy.d2(s1), root.sg_copy.d1(s2), root.sg_copy.d2(s2) - -from root.sg.d1, root.sg.d2; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 - -``` -select d1.s1, d1.s2, d2.s3, d3.s4 - -into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) - -from root.sg; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 - -``` -select * into root.sg_bk.::(::) from root.sg.**; -``` - -##### 按设备对齐(使用 `ALIGN BY DEVICE`) - -###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 -``` - -select s1, s2, s3, s4 - -into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) - -from root.sg.d1, root.sg.d2, root.sg.d3 - -align by device; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 -``` - -select avg(s1), sum(s2) + sum(s3), count(s4) - -into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) - -from root.** - -align by device; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 -``` - -select * into ::(backup_${4}) from root.sg.** align by device; -``` - -#### 指定目标序列为对齐序列 -``` - -select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -``` -## 5. 运维语句 -生成对应的查询计划 -``` -explain select s1,s2 from root.sg.d1 -``` -执行对应的查询语句,并获取分析结果 -``` -explain analyze select s1,s2 from root.sg.d1 order by s1 -``` -## 6. 运算符 - -更多见文档[Operator-and-Expression](./Operator-and-Expression.md) - -### 6.1 算数运算符 - -更多见文档 [Arithmetic Operators and Functions](./Operator-and-Expression.md#算数运算符) - -```sql -select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 -``` - -### 6.2 比较运算符 - -更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较运算符) - -```sql -# Basic comparison operators -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; - -# `BETWEEN ... AND ...` operator -select temperature from root.sg1.d1 where temperature between 36.5 and 40; -select temperature from root.sg1.d1 where temperature not between 36.5 and 40; - -# Fuzzy matching operator: Use `Like` for fuzzy matching -select * from root.sg.d1 where value like '%cc%' -select * from root.sg.device where value like '_b_' - -# Fuzzy matching operator: Use `Regexp` for fuzzy matching -select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -select b, b like '1%', b regexp '[0-2]' from root.test; - -# `IS NULL` operator -select code from root.sg1.d1 where temperature is null; -select code from root.sg1.d1 where temperature is not null; - -# `IN` operator -select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -select a, a in (1, 2) from root.test; -``` - -### 6.3 逻辑运算符 - -更多见文档[Logical Operators](./Operator-and-Expression.md#逻辑运算符) - -```sql -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; -``` - -## 7. 内置函数 - -更多见文档[Operator-and-Expression](./Operator-and-Expression.md#聚合函数) - -### 7.1 Aggregate Functions - -更多见文档[Aggregate Functions](./Operator-and-Expression.md#聚合函数) - -```sql -select count(status) from root.ln.wf01.wt01; - -select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; -select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; - -select time_duration(s1) from root.db.d1; -``` - -### 7.2 算数函数 - -更多见文档[Arithmetic Operators and Functions](./Operator-and-Expression.md#数学函数) - -```sql -select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; -select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; -``` - -### 7.3 比较函数 - -更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较函数) - -```sql -select ts, on_off(ts, 'threshold'='2') from root.test; -select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; -``` - -### 7.4 字符串处理函数 - -更多见文档[String Processing](./Operator-and-Expression.md#字符串函数) - -```sql -select s1, string_contains(s1, 's'='warn') from root.sg1.d4; -select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; -select s1, length(s1) from root.sg1.d1 -select s1, locate(s1, "target"="1") from root.sg1.d1 -select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 -select s1, startswith(s1, "target"="1") from root.sg1.d1 -select s1, endswith(s1, "target"="1") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 -select s1, substring(s1 from 1 for 2) from root.sg1.d1 -select s1, replace(s1, 'es', 'tt') from root.sg1.d1 -select s1, upper(s1) from root.sg1.d1 -select s1, lower(s1) from root.sg1.d1 -select s3, trim(s3) from root.sg1.d1 -select s1, s2, strcmp(s1, s2) from root.sg1.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 -select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 -select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 -``` - -### 7.5 数据类型转换函数 - -更多见文档[Data Type Conversion Function](./Operator-and-Expression.md#数据类型转换函数) - -```sql -SELECT cast(s1 as INT32) from root.sg -``` - -### 7.6 常序列生成函数 - -更多见文档[Constant Timeseries Generating Functions](./Operator-and-Expression.md#常序列生成函数) - -```sql -select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; -``` - -### 7.7 选择函数 - -更多见文档[Selector Functions](./Operator-and-Expression.md#选择函数) - -```sql -select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; -``` - -### 7.8 区间查询函数 - -更多见文档[Continuous Interval Functions](./Operator-and-Expression.md#区间查询函数) - -```sql -select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; -``` - -### 7.9 趋势计算函数 - -更多见文档[Variation Trend Calculation Functions](./Operator-and-Expression.md#趋势计算函数) - -```sql -select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; - -SELECT DIFF(s1), DIFF(s2) from root.test; -SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; -``` - -### 7.10 采样函数 - -更多见文档[Sample Functions](./Operator-and-Expression.md#采样函数)。 -### 7.11 时间序列处理函数 - -更多见文档[Sample Functions](./Operator-and-Expression.md#时间序列处理函数)。 - -```sql -select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; -select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; -select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; -select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; - -select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 -select M4(s1,'windowSize'='10') from root.vehicle.d1 -``` - -### 7.12 时间序列处理函数 - -更多见文档[Time-Series](./Operator-and-Expression.md#时间序列处理函数) - -```sql -select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 -``` - -## 8. 数据质量函数库 - -更多见文档[UDF-Libraries](../SQL-Manual/UDF-Libraries.md) - -### 8.1 数据质量 - -更多见文档[Data-Quality](../SQL-Manual/UDF-Libraries.md#数据质量) - -```sql -# Completeness -select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Consistency -select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Timeliness -select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Validity -select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Accuracy -select Accuracy(t1,t2,t3,m1,m2,m3) from root.test -``` - -### 8.2 数据画像 - -更多见文档[Data-Profiling](../SQL-Manual/UDF-Libraries.md#数据画像) - -```sql -# ACF -select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 - -# Distinct -select distinct(s2) from root.test.d2 - -# Histogram -select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 - -# Integral -select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 -select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 - -# IntegralAvg -select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 - -# Mad -select mad(s0) from root.test -select mad(s0, "error"="0.01") from root.test - -# Median -select median(s0, "error"="0.01") from root.test - -# MinMax -select minmax(s1) from root.test - -# Mode -select mode(s2) from root.test.d2 - -# MvAvg -select mvavg(s1, "window"="3") from root.test - -# PACF -select pacf(s1, "lag"="5") from root.test - -# Percentile -select percentile(s0, "rank"="0.2", "error"="0.01") from root.test - -# Quantile -select quantile(s0, "rank"="0.2", "K"="800") from root.test - -# Period -select period(s1) from root.test.d3 - -# QLB -select QLB(s1) from root.test.d1 - -# Resample -select resample(s1,'every'='5m','interp'='linear') from root.test.d1 -select resample(s1,'every'='30m','aggr'='first') from root.test.d1 -select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 - -# Sample -select sample(s1,'method'='reservoir','k'='5') from root.test.d1 -select sample(s1,'method'='isometric','k'='5') from root.test.d1 - -# Segment -select segment(s1, "error"="0.1") from root.test - -# Skew -select skew(s1) from root.test.d1 - -# Spline -select spline(s1, "points"="151") from root.test - -# Spread -select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 - -# Stddev -select stddev(s1) from root.test.d1 - -# ZScore -select zscore(s1) from root.test -``` - -### 8.3 异常检测 - -更多见文档[Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#异常检测) - -```sql -# IQR -select iqr(s1) from root.test - -# KSigma -select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# LOF -select lof(s1,s2) from root.test.d1 where time<1000 -select lof(s1, "method"="series") from root.test.d1 where time<1000 - -# MissDetect -select missdetect(s2,'minlen'='10') from root.test.d2 - -# Range -select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# TwoSidedFilter -select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test - -# Outlier -select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test - -# MasterTrain -select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test - -# MasterDetect -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test -``` - -### 8.4 频域分析 - -更多见文档[Frequency-Domain](../SQL-Manual/UDF-Libraries.md#频域分析) - -```sql -# Conv -select conv(s1,s2) from root.test.d2 - -# Deconv -select deconv(s3,s2) from root.test.d2 -select deconv(s3,s2,'result'='remainder') from root.test.d2 - -# DWT -select dwt(s1,"method"="haar") from root.test.d1 - -# FFT -select fft(s1) from root.test.d1 -select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 - -# HighPass -select highpass(s1,'wpass'='0.45') from root.test.d1 - -# IFFT -select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 - -# LowPass -select lowpass(s1,'wpass'='0.45') from root.test.d1 - -# Envelope -select envelope(s1) from root.test.d1 -``` - -### 8.5 数据匹配 - -更多见文档[Data-Matching](../SQL-Manual/UDF-Libraries.md#数据匹配) - -```sql -# Cov -select cov(s1,s2) from root.test.d2 - -# DTW -select dtw(s1,s2) from root.test.d2 - -# Pearson -select pearson(s1,s2) from root.test.d2 - -# PtnSym -select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 - -# XCorr -select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 -``` - -### 8.6 数据修复 - -更多见文档[Data-Repairing](../SQL-Manual/UDF-Libraries.md#数据修复) - -```sql -# TimestampRepair -select timestamprepair(s1,'interval'='10000') from root.test.d2 -select timestamprepair(s1) from root.test.d2 - -# ValueFill -select valuefill(s1) from root.test.d2 -select valuefill(s1,"method"="previous") from root.test.d2 - -# ValueRepair -select valuerepair(s1) from root.test.d2 -select valuerepair(s1,'method'='LsGreedy') from root.test.d2 - -# MasterRepair -select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test - -# SeasonalRepair -select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 -select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 -``` - -### 8.7 序列发现 - -更多见文档[Series-Discovery](../SQL-Manual/UDF-Libraries.md#序列发现) - -```sql -# ConsecutiveSequences -select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 -select consecutivesequences(s1,s2) from root.test.d1 - -# ConsecutiveWindows -select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 -``` - -### 8.8 机器学习 - -更多见文档[Machine-Learning](../SQL-Manual/UDF-Libraries.md#机器学习) - -```sql -# AR -select ar(s0,"p"="2") from root.test.d0 - -# Representation -select representation(s0,"tb"="3","vb"="2") from root.test.d0 - -# RM -select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 -``` - -## 9. Lambda 表达式 - -更多见文档[Lambda](./Operator-and-Expression.md#lambda-表达式) - -```sql -select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` -``` - -## 10. 条件表达式 - -更多见文档[Conditional Expressions](./Operator-and-Expression.md#条件表达式) - -```sql -select T, P, case -when 1000=1050 then "bad temperature" -when P<=1000000 or P>=1100000 then "bad pressure" -end as `result` -from root.test1 - -select str, case -when str like "%cc%" then "has cc" -when str like "%dd%" then "has dd" -else "no cc and dd" end as `result` -from root.test2 - -select -count(case when x<=1 then 1 end) as `(-∞,1]`, -count(case when 1 -[RESAMPLE - [EVERY ] - [BOUNDARY ] - [RANGE [, end_time_offset]] -] -[TIMEOUT POLICY BLOCKED|DISCARD] -BEGIN - SELECT CLAUSE - INTO CLAUSE - FROM CLAUSE - [WHERE CLAUSE] - [GROUP BY([, ]) [, level = ]] - [HAVING CLAUSE] - [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] - [LIMIT rowLimit OFFSET rowOffset] - [ALIGN BY DEVICE] -END -``` - -#### 配置连续查询执行的周期性间隔 -```sql -CREATE CONTINUOUS QUERY cq1 - -RESAMPLE EVERY 20s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - -END - - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 配置连续查询的时间窗口大小 -``` -CREATE CONTINUOUS QUERY cq2 - -RESAMPLE RANGE 40s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - -END - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 同时配置连续查询执行的周期性间隔和时间窗口大小 -```sql -CREATE CONTINUOUS QUERY cq3 - -RESAMPLE EVERY 20s RANGE 40s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - - FILL(100.0) - -END - - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 配置连续查询每次查询执行时间窗口的结束时间 -```sql -CREATE CONTINUOUS QUERY cq4 - -RESAMPLE EVERY 20s RANGE 40s, 20s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - - FILL(100.0) - -END - - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 没有GROUP BY TIME子句的连续查询 -```sql -CREATE CONTINUOUS QUERY cq5 - -RESAMPLE EVERY 20s - -BEGIN - - SELECT temperature + 1 - - INTO root.precalculated_sg.::(temperature) - - FROM root.ln.*.* - - align by device - -END - - - -\> SELECT temperature from root.precalculated_sg.*.* align by device; -``` -### 11.2 连续查询的管理 - -#### 查询系统已有的连续查询 - -展示集群中所有的已注册的连续查询 -```sql -SHOW (CONTINUOUS QUERIES | CQS) -``` -```sql -SHOW CONTINUOUS QUERIES; -``` -#### 删除已有的连续查询 - -删除指定的名为cq_id的连续查询: - -```sql -DROP (CONTINUOUS QUERY | CQ) -``` -```sql -DROP CONTINUOUS QUERY s1_count_cq; -``` -#### 作为子查询的替代品 - -1. 创建一个连续查询 -```sql -CREATE CQ s1_count_cq - -BEGIN - -​ SELECT count(s1) - -​ INTO root.sg_count.d.count_s1 - -​ FROM root.sg.d - -​ GROUP BY(30m) - -END -``` -1. 查询连续查询的结果 -```sql -SELECT avg(count_s1) from root.sg_count.d; -``` -## 12. 用户自定义函数 - -### 12.1 UDFParameters -```sql -SELECT UDF(s1, s2, 'key1'='iotdb', 'key2'='123.45') FROM root.sg.d; -``` -### 12.2 UDF 注册 - -```sql -CREATE FUNCTION AS (USING URI URI-STRING)? -``` - -#### 不指定URI -```sql -CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' -``` -#### 指定URI -```sql -CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' USING URI 'http://jar/example.jar' -``` -### 12.3 UDF 卸载 - -```sql -DROP FUNCTION -``` -```sql -DROP FUNCTION example -``` -### 12.4 UDF 查询 - -#### 带自定义输入参数的查询 -```sql -SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; -``` -```sql -SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; -``` -#### 与其他查询的嵌套查询 -```sql -SELECT s1, s2, example(s1, s2) FROM root.sg.d1; - -SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; - -SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; - -SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; -``` -### 12.5 查看所有注册的 UDF -```sql -SHOW FUNCTIONS -``` -## 13. 权限管理 - -### 13.1 用户与角色相关 - -- 创建用户(需 MANAGE_USER 权限) - - -```SQL -CREATE USER -eg: CREATE USER user1 'passwd' -``` - -- 删除用户 (需 MANEGE_USER 权限) - - -```SQL -DROP USER -eg: DROP USER user1 -``` - -- 创建角色 (需 MANAGE_ROLE 权限) - -```SQL -CREATE ROLE -eg: CREATE ROLE role1 -``` - -- 删除角色 (需 MANAGE_ROLE 权限) - - -```SQL -DROP ROLE -eg: DROP ROLE role1 -``` - -- 赋予用户角色 (需 MANAGE_ROLE 权限) - - -```SQL -GRANT ROLE TO -eg: GRANT ROLE admin TO user1 -``` - -- 移除用户角色 (需 MANAGE_ROLE 权限) - - -```SQL -REVOKE ROLE FROM -eg: REVOKE ROLE admin FROM user1 -``` - -- 列出所有用户 (需 MANEGE_USER 权限) - -```SQL -LIST USER -``` - -- 列出所有角色 (需 MANAGE_ROLE 权限) - -```SQL -LIST ROLE -``` - -- 列出指定角色下所有用户 (需 MANEGE_USER 权限) - -```SQL -LIST USER OF ROLE -eg: LIST USER OF ROLE roleuser -``` - -- 列出指定用户下所有角色 - -用户可以列出自己的角色,但列出其他用户的角色需要拥有 MANAGE_ROLE 权限。 - -```SQL -LIST ROLE OF USER -eg: LIST ROLE OF USER tempuser -``` - -- 列出用户所有权限 - -用户可以列出自己的权限信息,但列出其他用户的权限需要拥有 MANAGE_USER 权限。 - -```SQL -LIST PRIVILEGES OF USER ; -eg: LIST PRIVILEGES OF USER tempuser; +--- +redirectTo: SQL-Manual_apache.html +--- + \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_apache.md b/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_apache.md new file mode 100644 index 000000000..72b34374b --- /dev/null +++ b/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_apache.md @@ -0,0 +1,1973 @@ +# SQL手册 + +## 1. 元数据操作 + +### 1.1 数据库管理 + +#### 创建数据库 + +```sql +CREATE DATABASE root.ln +``` + +#### 查看数据库 + +```sql +show databases +show databases root.* +show databases root.** +``` + +#### 删除数据库 + +```sql +DELETE DATABASE root.ln +DELETE DATABASE root.sgcc +DELETE DATABASE root.** +``` + +#### 统计数据库数量 + +```sql +count databases +count databases root.* +count databases root.sgcc.* +count databases root.sgcc +``` + +### 1.2 时间序列管理 + +#### 创建时间序列 + +```sql +create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- 简化版 + +```sql +create timeseries root.ln.wf01.wt01.status BOOLEAN +create timeseries root.ln.wf01.wt01.temperature FLOAT +create timeseries root.ln.wf02.wt02.hardware TEXT +create timeseries root.ln.wf02.wt02.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature FLOAT +``` + +- 错误提示 + +```sql +create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +> error: encoding TS_2DIFF does not support BOOLEAN +``` + +#### 创建对齐时间序列 + +```sql +CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOAT) +``` + +#### 删除时间序列 + +```sql +delete timeseries root.ln.wf01.wt01.status +delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +delete timeseries root.ln.wf02.* +drop timeseries root.ln.wf02.* +``` + +#### 查看时间序列 + +```sql +SHOW TIMESERIES +SHOW TIMESERIES +SHOW TIMESERIES root.** +SHOW TIMESERIES root.ln.** +SHOW TIMESERIES root.ln.** limit 10 offset 10 +SHOW TIMESERIES root.ln.** where timeseries contains 'wf01.wt' +SHOW TIMESERIES root.ln.** where dataType=FLOAT +SHOW TIMESERIES root.ln.** where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +SHOW LATEST TIMESERIES +``` + +#### 统计时间序列数量 + +```sql +COUNT TIMESERIES root.** +COUNT TIMESERIES root.ln.** +COUNT TIMESERIES root.ln.*.*.status +COUNT TIMESERIES root.ln.wf01.wt01.status +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +COUNT TIMESERIES root.** WHERE time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +COUNT TIMESERIES root.** GROUP BY LEVEL=1 +COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +#### 标签点管理 + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +- 重命名标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +- 重新设置标签或属性的值 + +```sql +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +- 删除已经存在的标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +- 添加新的标签 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +- 添加新的属性 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +- 更新插入别名,标签和属性 + +```sql +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +- 使用标签作为过滤条件查询时间序列 + +```sql +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +返回给定路径的下的所有满足条件的时间序列信息: + +```sql +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- 使用标签作为过滤条件统计时间序列数量 + +```sql +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +返回给定路径的下的所有满足条件的时间序列的数量: + +```sql +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +创建对齐时间序列: + +```sql +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +支持查询: + +```sql +show timeseries where TAGS(tag1)='v1' +``` + +### 1.3 时间序列路径管理 + +#### 查看路径的所有子路径 + +```sql +SHOW CHILD PATHS pathPattern +- 查询 root.ln 的下一层:show child paths root.ln +- 查询形如 root.xx.xx.xx 的路径:show child paths root.*.* +``` +#### 查看路径的所有子节点 + +```sql +SHOW CHILD NODES pathPattern + +- 查询 root 的下一层:show child nodes root +- 查询 root.ln 的下一层 :show child nodes root.ln +``` +#### 查看设备 + +```sql +IoTDB> show devices + +IoTDB> show devices root.ln.** + +IoTDB> show devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +##### 查看设备及其 database 信息 + +```sql +IoTDB> show devices with database + +IoTDB> show devices root.ln.** with database +``` +#### 统计节点数 + +```sql +IoTDB > COUNT NODES root.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.wf01.* LEVEL=3 + +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` +#### 统计设备数量 + +```sql + +IoTDB> count devices + +IoTDB> count devices root.ln.** + +IoTDB> count devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +### 1.4 设备模板管理 + + +![img](/img/%E6%A8%A1%E6%9D%BF.png) +![img](/img/template.jpg) + + + +#### 创建设备模板 + +```Go +CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' +``` + +创建包含两个非对齐序列的设备模板 +```sql +IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) +``` +创建包含一组对齐序列的设备模板 +```sql +IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) +``` +#### 挂载设备模板 +```sql +IoTDB> set DEVICE TEMPLATE t1 to root.sg1 +``` +#### 激活设备模板 +```sql +IoTDB> create timeseries using DEVICE TEMPLATE on root.sg1.d1 + +IoTDB> set DEVICE TEMPLATE t1 to root.sg1.d1 + +IoTDB> set DEVICE TEMPLATE t2 to root.sg1.d2 + +IoTDB> create timeseries using device template on root.sg1.d1 + +IoTDB> create timeseries using device template on root.sg1.d2 +``` +#### 查看设备模板 +```sql +IoTDB> show device templates +``` +- 查看某个设备模板下的物理量 +```sql +IoTDB> show nodes in device template t1 +``` +- 查看挂载了某个设备模板的路径 +```sql +IoTDB> show paths set device template t1 +``` +- 查看使用了某个设备模板的路径(即模板在该路径上已激活,序列已创建) +```sql +IoTDB> show paths using device template t1 +``` +#### 解除设备模板 +```sql +IoTDB> delete timeseries of device template t1 from root.sg1.d1 +``` +```sql +IoTDB> deactivate device template t1 from root.sg1.d1 +``` +批量处理 +```sql +IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* +``` +```sql +IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* +``` +#### 卸载设备模板 +```sql +IoTDB> unset device template t1 from root.sg1.d1 +``` +#### 删除设备模板 +```sql +IoTDB> drop device template t1 +``` +### 1.5 数据存活时间管理 + +#### 设置 TTL +```sql +IoTDB> set ttl to root.ln 3600000 +``` +```sql +IoTDB> set ttl to root.sgcc.** 3600000 +``` +```sql +IoTDB> set ttl to root.** 3600000 +``` +#### 取消 TTL +```sql +IoTDB> unset ttl from root.ln +``` +```sql +IoTDB> unset ttl from root.sgcc.** +``` +```sql +IoTDB> unset ttl from root.** +``` + +#### 显示 TTL +```sql +IoTDB> SHOW ALL TTL +``` +```sql +IoTDB> SHOW TTL ON pathPattern +``` +```sql +IoTDB> show DEVICES +``` +## 2. 写入数据 + +### 2.1 写入单列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1'),(2, 'v1') +``` +### 2.2 写入多列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) values (2, false, 'v2') +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` +### 2.3 使用服务器时间戳 +```sql +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` +### 2.4 写入对齐时间序列数据 +```sql +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(1, 1, 1) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +``` +```sql +IoTDB > select * from root.sg1.d1 +``` +### 2.5 加载 TsFile 文件数据 + +load '' [sglevel=int][onSuccess=delete/none] + +#### 通过指定文件路径(绝对路径)加载单 tsfile 文件 + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +#### 通过指定文件夹路径(绝对路径)批量加载文件 + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +## 3. 删除数据 + +### 3.1 删除单列数据 +```sql +delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time < 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time > 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time >= 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time = 20 +``` +出错: +```sql +delete from root.ln.wf02.wt02.status where time > 4 or time < 0 + +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic + +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +``` + +删除时间序列中的所有数据: +```sql +delete from root.ln.wf02.wt02.status +``` +### 3.2 删除多列数据 +```sql +delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +``` +声明式的编程方式: +```sql +IoTDB> delete from root.ln.wf03.wt02.status where time < now() + +Msg: The statement is executed successfully. +``` +## 4. 数据查询 + +### 4.1 基础查询 + +#### 时间过滤查询 +```sql +select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` +#### 根据一个时间区间选择多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 按照多个时间区间选择同一设备的多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 按照多个时间区间选择不同设备的多列数据 +```sql +select wf01.wt01.status, wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 根据时间降序返回结果集 +```sql +select * from root.ln.** where time > 1 order by time desc limit 10; +``` +### 4.2 选择表达式 + +#### 使用别名 +```sql +select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` +#### 运算符 + +#### 函数 + +不支持: +```sql +select s1, count(s1) from root.sg.d1; + +select sin(s1), count(s1) from root.sg.d1; + +select s1, count(s1) from root.sg.d1 group by ([10,100),10ms); +``` +##### 时间序列查询嵌套表达式 + +示例 1: +```sql +select a, + +​ b, + +​ ((a + 1) * 2 - 1) % 2 + 1.5, + +​ sin(a + sin(a + sin(b))), + +​ -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 + +from root.sg1; +``` +示例 2: +```sql +select (a + b) * 2 + sin(a) from root.sg + +示例 3: + +select (a + *) / 2 from root.sg1 + +示例 4: + +select (a + b) * 3 from root.sg, root.ln +``` +##### 聚合查询嵌套表达式 + +示例 1: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) + +from root.ln.wf01.wt01; +``` +示例 2: +```sql +select avg(*), + +​ (avg(*) + 1) * 3 / 2 -1 + +from root.sg1 +``` +示例 3: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) as custom_sum + +from root.ln.wf01.wt01 + +GROUP BY([10, 90), 10ms); +``` +#### 最新点查询 + +SQL 语法: + +```Go +select last [COMMA ]* from < PrefixPath > [COMMA < PrefixPath >]* [ORDER BY TIMESERIES (DESC | ASC)?] +``` + +查询 root.ln.wf01.wt01.status 的最新数据点 +```sql +IoTDB> select last status from root.ln.wf01.wt01 +``` +查询 root.ln.wf01.wt01 下 status,temperature 时间戳大于等于 2017-11-07T23:50:00 的最新数据点 +```sql +IoTDB> select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +``` +查询 root.ln.wf01.wt01 下所有序列的最新数据点,并按照序列名降序排列 +```sql +IoTDB> select last * from root.ln.wf01.wt01 order by timeseries desc; +``` +### 4.3 查询过滤条件 + +#### 时间过滤条件 + +选择时间戳大于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +``` +选择时间戳等于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +``` +选择时间区间 [2017-11-01T00:05:00.000, 2017-11-01T00:12:00.000) 内的数据: +```sql +select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 值过滤条件 + +选择值大于 36.5 的数据: +```sql +select temperature from root.sg1.d1 where temperature > 36.5; +``` +选择值等于 true 的数据: +```sql +select status from root.sg1.d1 where status = true; +``` +选择区间 [36.5,40] 内或之外的数据: +```sql +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +``` +```sql +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +``` +选择值在特定范围内的数据: +```sql +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +``` +选择值在特定范围外的数据: +```sql +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +``` +选择值为空的数据: +```sql +select code from root.sg1.d1 where temperature is null; +``` +选择值为非空的数据: +```sql +select code from root.sg1.d1 where temperature is not null; +``` +#### 模糊查询 + +查询 `root.sg.d1` 下 `value` 含有`'cc'`的数据 +```sql +IoTDB> select * from root.sg.d1 where value like '%cc%' +``` +查询 `root.sg.d1` 下 `value` 中间为 `'b'`、前后为任意单个字符的数据 +```sql +IoTDB> select * from root.sg.device where value like '_b_' +``` +查询 root.sg.d1 下 value 值为26个英文字符组成的字符串 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +``` + +查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 4.4 分段分组聚合 + +#### 未指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` +#### 指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` +滑动步长可以小于聚合窗口 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-01 10:00:00), 4h, 2h); +``` +#### 按照自然月份的时间区间分组聚合查询 +```sql +select count(status) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +每个时间间隔窗口内都有数据 +```sql +select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +#### 左开右闭区间 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` +#### 与分组聚合混合使用 + +统计降采样后的数据点个数 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +``` +加上滑动 Step 的降采样后的结果也可以汇总 +```sql +select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` +#### 路径层级分组聚合 + +统计不同 database 下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1 +``` +统计不同设备下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 3 +``` +统计不同 database 下的不同设备中 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1, 3 +``` +查询所有序列下温度传感器 temperature 的最大值 +```sql +select max_value(temperature) from root.** group by level = 0 +``` +查询某一层级下所有传感器拥有的总数据点数 +```sql +select count(*) from root.ln.** group by level = 2 +``` +#### 标签分组聚合 + +##### 单标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` +##### 多标签聚合查询 +```sql +SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` +##### 基于时间区间的标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` +#### 差值分段聚合 +```sql +group by variation(controlExpression[,delta][,ignoreNull=true/false]) +``` +##### delta=0时的等值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +``` +指定ignoreNull为false +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +``` +##### delta!=0时的差值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +``` +#### 条件分段聚合 +```sql +group by condition(predict,[keep>/>=/=/<=/<]threshold,[,ignoreNull=true/false]) +``` +查询至少连续两行以上的charging_status=1的数据 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=true) +``` +当设置`ignoreNull`为false时,遇到null值为将其视为一个不满足条件的行,得到结果原先的分组被含null的行拆分 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=false) +``` +#### 会话分段聚合 +```sql +group by session(timeInterval) +``` +按照不同的时间单位设定时间间隔 +```sql +select __endTime,count(*) from root.** group by session(1d) +``` +和`HAVING`、`ALIGN BY DEVICE`共同使用 +```sql +select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` +#### 点数分段聚合 +```sql +group by count(controlExpression, size[,ignoreNull=true/false]) +``` +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) + +当使用ignoreNull将null值也考虑进来 +```sql +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` +### 4.5 聚合结果过滤 + +不正确的: +```sql +select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 + +select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 + +select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 + +select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` +SQL 示例: +```sql + select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 2; + + select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device; +``` +### 4.6 结果集补空值 +```sql +FILL '(' PREVIOUS | LINEAR | constant (, interval=DURATION_LITERAL)? ')' +``` +#### `PREVIOUS` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` +#### `PREVIOUS` 填充并指定填充超时阈值 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` +#### `LINEAR` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` +#### 常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +``` +使用 `BOOLEAN` 类型的常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` +### 4.7 查询结果分页 + +#### 按行分页 + +基本的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 10 +``` +带 `OFFSET` 的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +``` +`LIMIT` 子句与 `WHERE` 子句结合 +```sql +select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 +``` +`LIMIT` 子句与 `GROUP BY` 子句组合 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 +``` +#### 按列分页 + +基本的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +``` +带 `SOFFSET` 的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `GROUP BY` 子句结合 +```sql +select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `LIMIT` 子句结合 +```sql +select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` +### 4.8 排序 + +时间对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` +设备对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +``` +在时间戳相等时按照设备名排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +``` +没有显式指定时 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +对聚合后的结果进行排序 +```sql +select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` +### 4.9 查询对齐模式 + +#### 按设备对齐 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +### 4.10 查询写回(SELECT INTO) + +#### 整体描述 +```sql +selectIntoStatement + +​ : SELECT + +​ resultColumn [, resultColumn] ... + +​ INTO intoItem [, intoItem] ... + +​ FROM prefixPath [, prefixPath] ... + +​ [WHERE whereCondition] + +​ [GROUP BY groupByTimeClause, groupByLevelClause] + +​ [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + +​ [LIMIT rowLimit OFFSET rowOffset] + +​ [ALIGN BY DEVICE] + +​ ; + + + +intoItem + +​ : [ALIGNED] intoDevicePath '(' intoMeasurementName [',' intoMeasurementName]* ')' + +​ ; +``` +按时间对齐,将 `root.sg` database 下四条序列的查询结果写入到 `root.sg_copy` database 下指定的四条序列中 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +``` +按时间对齐,将聚合查询的结果存储到指定序列中 +```sql +IoTDB> select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +``` +按设备对齐 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +按设备对齐,将表达式计算的结果存储到指定序列中 +```sql +IoTDB> select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` +#### 使用变量占位符 + +##### 按时间对齐(默认) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2 + +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) + +from root.sg.d1, root.sg.d2; +``` + +该语句等价于: +``` + +select s1, s2 + +into root.sg_copy.d1(s1), root.sg_copy.d2(s1), root.sg_copy.d1(s2), root.sg_copy.d2(s2) + +from root.sg.d1, root.sg.d2; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 + +``` +select d1.s1, d1.s2, d2.s3, d3.s4 + +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) + +from root.sg; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 + +``` +select * into root.sg_bk.::(::) from root.sg.**; +``` + +##### 按设备对齐(使用 `ALIGN BY DEVICE`) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2, s3, s4 + +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) + +from root.sg.d1, root.sg.d2, root.sg.d3 + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 +``` + +select avg(s1), sum(s2) + sum(s3), count(s4) + +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) + +from root.** + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select * into ::(backup_${4}) from root.sg.** align by device; +``` + +#### 指定目标序列为对齐序列 +``` + +select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +## 5. 运维语句 +生成对应的查询计划 +``` +explain select s1,s2 from root.sg.d1 +``` +执行对应的查询语句,并获取分析结果 +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 6. 运算符 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md) + +### 6.1 算数运算符 + +更多见文档 [Arithmetic Operators and Functions](./Operator-and-Expression.md#算数运算符) + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 6.2 比较运算符 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较运算符) + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 6.3 逻辑运算符 + +更多见文档[Logical Operators](./Operator-and-Expression.md#逻辑运算符) + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 7. 内置函数 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md#聚合函数) + +### 7.1 Aggregate Functions + +更多见文档[Aggregate Functions](./Operator-and-Expression.md#聚合函数) + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 7.2 算数函数 + +更多见文档[Arithmetic Operators and Functions](./Operator-and-Expression.md#数学函数) + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 7.3 比较函数 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较函数) + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 7.4 字符串处理函数 + +更多见文档[String Processing](./Operator-and-Expression.md#字符串函数) + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 7.5 数据类型转换函数 + +更多见文档[Data Type Conversion Function](./Operator-and-Expression.md#数据类型转换函数) + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 7.6 常序列生成函数 + +更多见文档[Constant Timeseries Generating Functions](./Operator-and-Expression.md#常序列生成函数) + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 7.7 选择函数 + +更多见文档[Selector Functions](./Operator-and-Expression.md#选择函数) + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 7.8 区间查询函数 + +更多见文档[Continuous Interval Functions](./Operator-and-Expression.md#区间查询函数) + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 7.9 趋势计算函数 + +更多见文档[Variation Trend Calculation Functions](./Operator-and-Expression.md#趋势计算函数) + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 7.10 采样函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#采样函数)。 +### 7.11 时间序列处理函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#时间序列处理函数)。 + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 7.12 时间序列处理函数 + +更多见文档[Time-Series](./Operator-and-Expression.md#时间序列处理函数) + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 8. 数据质量函数库 + +更多见文档[UDF-Libraries](../SQL-Manual/UDF-Libraries.md) + +### 8.1 数据质量 + +更多见文档[Data-Quality](../SQL-Manual/UDF-Libraries.md#数据质量) + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 8.2 数据画像 + +更多见文档[Data-Profiling](../SQL-Manual/UDF-Libraries.md#数据画像) + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 8.3 异常检测 + +更多见文档[Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#异常检测) + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 8.4 频域分析 + +更多见文档[Frequency-Domain](../SQL-Manual/UDF-Libraries.md#频域分析) + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 8.5 数据匹配 + +更多见文档[Data-Matching](../SQL-Manual/UDF-Libraries.md#数据匹配) + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 8.6 数据修复 + +更多见文档[Data-Repairing](../SQL-Manual/UDF-Libraries.md#数据修复) + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 8.7 序列发现 + +更多见文档[Series-Discovery](../SQL-Manual/UDF-Libraries.md#序列发现) + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 8.8 机器学习 + +更多见文档[Machine-Learning](../SQL-Manual/UDF-Libraries.md#机器学习) + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 9. Lambda 表达式 + +更多见文档[Lambda](./Operator-and-Expression.md#lambda-表达式) + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 10. 条件表达式 + +更多见文档[Conditional Expressions](./Operator-and-Expression.md#条件表达式) + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +#### 配置连续查询执行的周期性间隔 +```sql +CREATE CONTINUOUS QUERY cq1 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询的时间窗口大小 +``` +CREATE CONTINUOUS QUERY cq2 + +RESAMPLE RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 同时配置连续查询执行的周期性间隔和时间窗口大小 +```sql +CREATE CONTINUOUS QUERY cq3 + +RESAMPLE EVERY 20s RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询每次查询执行时间窗口的结束时间 +```sql +CREATE CONTINUOUS QUERY cq4 + +RESAMPLE EVERY 20s RANGE 40s, 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 没有GROUP BY TIME子句的连续查询 +```sql +CREATE CONTINUOUS QUERY cq5 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT temperature + 1 + + INTO root.precalculated_sg.::(temperature) + + FROM root.ln.*.* + + align by device + +END + + + +\> SELECT temperature from root.precalculated_sg.*.* align by device; +``` +### 11.2 连续查询的管理 + +#### 查询系统已有的连续查询 + +展示集群中所有的已注册的连续查询 +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` +```sql +SHOW CONTINUOUS QUERIES; +``` +#### 删除已有的连续查询 + +删除指定的名为cq_id的连续查询: + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` +```sql +DROP CONTINUOUS QUERY s1_count_cq; +``` +#### 作为子查询的替代品 + +1. 创建一个连续查询 +```sql +CREATE CQ s1_count_cq + +BEGIN + +​ SELECT count(s1) + +​ INTO root.sg_count.d.count_s1 + +​ FROM root.sg.d + +​ GROUP BY(30m) + +END +``` +1. 查询连续查询的结果 +```sql +SELECT avg(count_s1) from root.sg_count.d; +``` +## 12. 用户自定义函数 + +### 12.1 UDFParameters +```sql +SELECT UDF(s1, s2, 'key1'='iotdb', 'key2'='123.45') FROM root.sg.d; +``` +### 12.2 UDF 注册 + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +#### 不指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' +``` +#### 指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' USING URI 'http://jar/example.jar' +``` +### 12.3 UDF 卸载 + +```sql +DROP FUNCTION +``` +```sql +DROP FUNCTION example +``` +### 12.4 UDF 查询 + +#### 带自定义输入参数的查询 +```sql +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +``` +```sql +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; +``` +#### 与其他查询的嵌套查询 +```sql +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; + +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; + +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; + +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` +### 12.5 查看所有注册的 UDF +```sql +SHOW FUNCTIONS +``` +## 13. 权限管理 + +### 13.1 用户与角色相关 + +- 创建用户(需 MANAGE_USER 权限) + + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- 删除用户 (需 MANEGE_USER 权限) + + +```SQL +DROP USER +eg: DROP USER user1 +``` + +- 创建角色 (需 MANAGE_ROLE 权限) + +```SQL +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- 删除角色 (需 MANAGE_ROLE 权限) + + +```SQL +DROP ROLE +eg: DROP ROLE role1 +``` + +- 赋予用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- 移除用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- 列出所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER +``` + +- 列出所有角色 (需 MANAGE_ROLE 权限) + +```SQL +LIST ROLE +``` + +- 列出指定角色下所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- 列出指定用户下所有角色 + +用户可以列出自己的角色,但列出其他用户的角色需要拥有 MANAGE_ROLE 权限。 + +```SQL +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- 列出用户所有权限 + +用户可以列出自己的权限信息,但列出其他用户的权限需要拥有 MANAGE_USER 权限。 + +```SQL +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; + +``` + +- 列出角色所有权限 + +用户可以列出自己具有的角色的权限信息,列出其他角色的权限需要有 MANAGE_ROLE 权限。 + +```SQL +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- 更新密码 + +用户可以更新自己的密码,但更新其他用户密码需要具备MANAGE_USER 权限。 + +```SQL +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 13.2 授权与取消授权 + +用户使用授权语句对赋予其他用户权限,语法如下: + +```SQL +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +用户使用取消授权语句可以将其他的权限取消,语法如下: + +```SQL +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + diff --git a/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_timecho.md b/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_timecho.md new file mode 100644 index 000000000..403546035 --- /dev/null +++ b/src/zh/UserGuide/Master/Tree/SQL-Manual/SQL-Manual_timecho.md @@ -0,0 +1,1898 @@ +# SQL手册 + +## 1. 元数据操作 + +### 1.1 数据库管理 + +#### 创建数据库 + +```sql +CREATE DATABASE root.ln +``` + +#### 查看数据库 + +```sql +show databases +show databases root.* +show databases root.** +``` + +#### 删除数据库 + +```sql +DELETE DATABASE root.ln +DELETE DATABASE root.sgcc +DELETE DATABASE root.** +``` + +#### 统计数据库数量 + +```sql +count databases +count databases root.* +count databases root.sgcc.* +count databases root.sgcc +``` + +### 1.2 时间序列管理 + +#### 创建时间序列 + +```sql +create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- 简化版 + +```sql +create timeseries root.ln.wf01.wt01.status BOOLEAN +create timeseries root.ln.wf01.wt01.temperature FLOAT +create timeseries root.ln.wf02.wt02.hardware TEXT +create timeseries root.ln.wf02.wt02.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature FLOAT +``` + +- 错误提示 + +```sql +create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +> error: encoding TS_2DIFF does not support BOOLEAN +``` + +#### 创建对齐时间序列 + +```sql +CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOAT) +``` + +#### 删除时间序列 + +```sql +delete timeseries root.ln.wf01.wt01.status +delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +delete timeseries root.ln.wf02.* +drop timeseries root.ln.wf02.* +``` + +#### 查看时间序列 + +```sql +SHOW TIMESERIES +SHOW TIMESERIES +SHOW TIMESERIES root.** +SHOW TIMESERIES root.ln.** +SHOW TIMESERIES root.ln.** limit 10 offset 10 +SHOW TIMESERIES root.ln.** where timeseries contains 'wf01.wt' +SHOW TIMESERIES root.ln.** where dataType=FLOAT +SHOW TIMESERIES root.ln.** where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +SHOW LATEST TIMESERIES +``` + +#### 统计时间序列数量 + +```sql +COUNT TIMESERIES root.** +COUNT TIMESERIES root.ln.** +COUNT TIMESERIES root.ln.*.*.status +COUNT TIMESERIES root.ln.wf01.wt01.status +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +COUNT TIMESERIES root.** WHERE time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +COUNT TIMESERIES root.** GROUP BY LEVEL=1 +COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +#### 标签点管理 + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +- 重命名标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +- 重新设置标签或属性的值 + +```sql +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +- 删除已经存在的标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +- 添加新的标签 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +- 添加新的属性 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +- 更新插入别名,标签和属性 + +```sql +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +- 使用标签作为过滤条件查询时间序列 + +```sql +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +返回给定路径的下的所有满足条件的时间序列信息: + +```sql +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- 使用标签作为过滤条件统计时间序列数量 + +```sql +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +返回给定路径的下的所有满足条件的时间序列的数量: + +```sql +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +创建对齐时间序列: + +```sql +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +支持查询: + +```sql +show timeseries where TAGS(tag1)='v1' +``` + +### 1.3 时间序列路径管理 + +#### 查看路径的所有子路径 + +```sql +SHOW CHILD PATHS pathPattern +- 查询 root.ln 的下一层:show child paths root.ln +- 查询形如 root.xx.xx.xx 的路径:show child paths root.*.* +``` +#### 查看路径的所有子节点 + +```sql +SHOW CHILD NODES pathPattern + +- 查询 root 的下一层:show child nodes root +- 查询 root.ln 的下一层 :show child nodes root.ln +``` +#### 查看设备 + +```sql +IoTDB> show devices + +IoTDB> show devices root.ln.** + +IoTDB> show devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +##### 查看设备及其 database 信息 + +```sql +IoTDB> show devices with database + +IoTDB> show devices root.ln.** with database +``` +#### 统计节点数 + +```sql +IoTDB > COUNT NODES root.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.wf01.* LEVEL=3 + +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` +#### 统计设备数量 + +```sql + +IoTDB> count devices + +IoTDB> count devices root.ln.** + +IoTDB> count devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` + +### 1.4 数据存活时间管理 + +#### 设置 TTL +```sql +IoTDB> set ttl to root.ln 3600000 +``` +```sql +IoTDB> set ttl to root.sgcc.** 3600000 +``` +```sql +IoTDB> set ttl to root.** 3600000 +``` +#### 取消 TTL +```sql +IoTDB> unset ttl from root.ln +``` +```sql +IoTDB> unset ttl from root.sgcc.** +``` +```sql +IoTDB> unset ttl from root.** +``` + +#### 显示 TTL +```sql +IoTDB> SHOW ALL TTL +``` +```sql +IoTDB> SHOW TTL ON pathPattern +``` +```sql +IoTDB> show DEVICES +``` +## 2. 写入数据 + +### 2.1 写入单列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1'),(2, 'v1') +``` +### 2.2 写入多列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) values (2, false, 'v2') +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` +### 2.3 使用服务器时间戳 +```sql +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` +### 2.4 写入对齐时间序列数据 +```sql +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(1, 1, 1) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +``` +```sql +IoTDB > select * from root.sg1.d1 +``` +### 2.5 加载 TsFile 文件数据 + +load '' [sglevel=int][onSuccess=delete/none] + +#### 通过指定文件路径(绝对路径)加载单 tsfile 文件 + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +#### 通过指定文件夹路径(绝对路径)批量加载文件 + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +## 3. 删除数据 + +### 3.1 删除单列数据 +```sql +delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time < 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time > 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time >= 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time = 20 +``` +出错: +```sql +delete from root.ln.wf02.wt02.status where time > 4 or time < 0 + +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic + +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +``` + +删除时间序列中的所有数据: +```sql +delete from root.ln.wf02.wt02.status +``` +### 3.2 删除多列数据 +```sql +delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +``` +声明式的编程方式: +```sql +IoTDB> delete from root.ln.wf03.wt02.status where time < now() + +Msg: The statement is executed successfully. +``` +## 4. 数据查询 + +### 4.1 基础查询 + +#### 时间过滤查询 +```sql +select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` +#### 根据一个时间区间选择多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 按照多个时间区间选择同一设备的多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 按照多个时间区间选择不同设备的多列数据 +```sql +select wf01.wt01.status, wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 根据时间降序返回结果集 +```sql +select * from root.ln.** where time > 1 order by time desc limit 10; +``` +### 4.2 选择表达式 + +#### 使用别名 +```sql +select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` +#### 运算符 + +#### 函数 + +不支持: +```sql +select s1, count(s1) from root.sg.d1; + +select sin(s1), count(s1) from root.sg.d1; + +select s1, count(s1) from root.sg.d1 group by ([10,100),10ms); +``` +##### 时间序列查询嵌套表达式 + +示例 1: +```sql +select a, + +​ b, + +​ ((a + 1) * 2 - 1) % 2 + 1.5, + +​ sin(a + sin(a + sin(b))), + +​ -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 + +from root.sg1; +``` +示例 2: +```sql +select (a + b) * 2 + sin(a) from root.sg + +示例 3: + +select (a + *) / 2 from root.sg1 + +示例 4: + +select (a + b) * 3 from root.sg, root.ln +``` +##### 聚合查询嵌套表达式 + +示例 1: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) + +from root.ln.wf01.wt01; +``` +示例 2: +```sql +select avg(*), + +​ (avg(*) + 1) * 3 / 2 -1 + +from root.sg1 +``` +示例 3: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) as custom_sum + +from root.ln.wf01.wt01 + +GROUP BY([10, 90), 10ms); +``` +#### 最新点查询 + +SQL 语法: + +```Go +select last [COMMA ]* from < PrefixPath > [COMMA < PrefixPath >]* [ORDER BY TIMESERIES (DESC | ASC)?] +``` + +查询 root.ln.wf01.wt01.status 的最新数据点 +```sql +IoTDB> select last status from root.ln.wf01.wt01 +``` +查询 root.ln.wf01.wt01 下 status,temperature 时间戳大于等于 2017-11-07T23:50:00 的最新数据点 +```sql +IoTDB> select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +``` + 查询 root.ln.wf01.wt01 下所有序列的最新数据点,并按照序列名降序排列 +```sql +IoTDB> select last * from root.ln.wf01.wt01 order by timeseries desc; +``` +### 4.3 查询过滤条件 + +#### 时间过滤条件 + +选择时间戳大于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +``` +选择时间戳等于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +``` +选择时间区间 [2017-11-01T00:05:00.000, 2017-11-01T00:12:00.000) 内的数据: +```sql +select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 值过滤条件 + +选择值大于 36.5 的数据: +```sql +select temperature from root.sg1.d1 where temperature > 36.5; +``` +选择值等于 true 的数据: +```sql +select status from root.sg1.d1 where status = true; +``` +选择区间 [36.5,40] 内或之外的数据: +```sql +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +``` +```sql +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +``` +选择值在特定范围内的数据: +```sql +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +``` +选择值在特定范围外的数据: +```sql +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +``` +选择值为空的数据: +```sql +select code from root.sg1.d1 where temperature is null; +``` +选择值为非空的数据: +```sql +select code from root.sg1.d1 where temperature is not null; +``` +#### 模糊查询 + +查询 `root.sg.d1` 下 `value` 含有`'cc'`的数据 +```sql +IoTDB> select * from root.sg.d1 where value like '%cc%' +``` +查询 `root.sg.d1` 下 `value` 中间为 `'b'`、前后为任意单个字符的数据 +```sql +IoTDB> select * from root.sg.device where value like '_b_' +``` +查询 root.sg.d1 下 value 值为26个英文字符组成的字符串 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +``` + +查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 4.4 分段分组聚合 + +#### 未指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` +#### 指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` +滑动步长可以小于聚合窗口 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-01 10:00:00), 4h, 2h); +``` +#### 按照自然月份的时间区间分组聚合查询 +```sql +select count(status) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +每个时间间隔窗口内都有数据 +```sql +select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +#### 左开右闭区间 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` +#### 与分组聚合混合使用 + +统计降采样后的数据点个数 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +``` +加上滑动 Step 的降采样后的结果也可以汇总 +```sql +select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` +#### 路径层级分组聚合 + +统计不同 database 下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1 +``` + 统计不同设备下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 3 +``` +统计不同 database 下的不同设备中 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1, 3 +``` +查询所有序列下温度传感器 temperature 的最大值 +```sql +select max_value(temperature) from root.** group by level = 0 +``` +查询某一层级下所有传感器拥有的总数据点数 +```sql +select count(*) from root.ln.** group by level = 2 +``` +#### 标签分组聚合 + +##### 单标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` +##### 多标签聚合查询 +```sql +SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` +##### 基于时间区间的标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` +#### 差值分段聚合 +```sql +group by variation(controlExpression[,delta][,ignoreNull=true/false]) +``` +##### delta=0时的等值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +``` +指定ignoreNull为false +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +``` +##### delta!=0时的差值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +``` +#### 条件分段聚合 +```sql +group by condition(predict,[keep>/>=/=/<=/<]threshold,[,ignoreNull=true/false]) +``` +查询至少连续两行以上的charging_status=1的数据 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=true) +``` +当设置`ignoreNull`为false时,遇到null值为将其视为一个不满足条件的行,得到结果原先的分组被含null的行拆分 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=false) +``` +#### 会话分段聚合 +```sql +group by session(timeInterval) +``` +按照不同的时间单位设定时间间隔 +```sql +select __endTime,count(*) from root.** group by session(1d) +``` +和`HAVING`、`ALIGN BY DEVICE`共同使用 +```sql +select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` +#### 点数分段聚合 +```sql +group by count(controlExpression, size[,ignoreNull=true/false]) +``` +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) + +当使用ignoreNull将null值也考虑进来 +```sql +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` +### 4.5 聚合结果过滤 + +不正确的: +```sql +select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 + +select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 + +select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 + +select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` +SQL 示例: +```sql + select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 2; + + select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device; +``` +### 4.6 结果集补空值 +```sql +FILL '(' PREVIOUS | LINEAR | constant (, interval=DURATION_LITERAL)? ')' +``` +#### `PREVIOUS` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` +#### `PREVIOUS` 填充并指定填充超时阈值 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` +#### `LINEAR` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` +#### 常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +``` +使用 `BOOLEAN` 类型的常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` +### 4.7 查询结果分页 + +#### 按行分页 + + 基本的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 10 +``` +带 `OFFSET` 的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +``` +`LIMIT` 子句与 `WHERE` 子句结合 +```sql +select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 +``` + `LIMIT` 子句与 `GROUP BY` 子句组合 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 +``` +#### 按列分页 + + 基本的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +``` +带 `SOFFSET` 的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `GROUP BY` 子句结合 +```sql +select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `LIMIT` 子句结合 +```sql +select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` +### 4.8 排序 + +时间对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` +设备对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +``` +在时间戳相等时按照设备名排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +``` +没有显式指定时 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +对聚合后的结果进行排序 +```sql +select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` +### 4.9 查询对齐模式 + +#### 按设备对齐 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +### 4.10 查询写回(SELECT INTO) + +#### 整体描述 +```sql +selectIntoStatement + +​ : SELECT + +​ resultColumn [, resultColumn] ... + +​ INTO intoItem [, intoItem] ... + +​ FROM prefixPath [, prefixPath] ... + +​ [WHERE whereCondition] + +​ [GROUP BY groupByTimeClause, groupByLevelClause] + +​ [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + +​ [LIMIT rowLimit OFFSET rowOffset] + +​ [ALIGN BY DEVICE] + +​ ; + + + +intoItem + +​ : [ALIGNED] intoDevicePath '(' intoMeasurementName [',' intoMeasurementName]* ')' + +​ ; +``` +按时间对齐,将 `root.sg` database 下四条序列的查询结果写入到 `root.sg_copy` database 下指定的四条序列中 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +``` +按时间对齐,将聚合查询的结果存储到指定序列中 +```sql +IoTDB> select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +``` +按设备对齐 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +按设备对齐,将表达式计算的结果存储到指定序列中 +```sql +IoTDB> select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` +#### 使用变量占位符 + +##### 按时间对齐(默认) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2 + +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) + +from root.sg.d1, root.sg.d2; +``` + +该语句等价于: +``` + +select s1, s2 + +into root.sg_copy.d1(s1), root.sg_copy.d2(s1), root.sg_copy.d1(s2), root.sg_copy.d2(s2) + +from root.sg.d1, root.sg.d2; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 + +``` +select d1.s1, d1.s2, d2.s3, d3.s4 + +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) + +from root.sg; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 + +``` +select * into root.sg_bk.::(::) from root.sg.**; +``` + +##### 按设备对齐(使用 `ALIGN BY DEVICE`) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2, s3, s4 + +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) + +from root.sg.d1, root.sg.d2, root.sg.d3 + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 +``` + +select avg(s1), sum(s2) + sum(s3), count(s4) + +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) + +from root.** + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select * into ::(backup_${4}) from root.sg.** align by device; +``` + +#### 指定目标序列为对齐序列 +``` + +select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +## 5. 运维语句 +生成对应的查询计划 +``` +explain select s1,s2 from root.sg.d1 +``` +执行对应的查询语句,并获取分析结果 +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 6. 运算符 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md) + +### 6.1 算数运算符 + +更多见文档 [Arithmetic Operators and Functions](./Operator-and-Expression.md#算数运算符) + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 6.2 比较运算符 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较运算符) + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 6.3 逻辑运算符 + +更多见文档[Logical Operators](./Operator-and-Expression.md#逻辑运算符) + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 7. 内置函数 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md#聚合函数) + +### 7.1 Aggregate Functions + +更多见文档[Aggregate Functions](./Operator-and-Expression.md#聚合函数) + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 7.2 算数函数 + +更多见文档[Arithmetic Operators and Functions](./Operator-and-Expression.md#数学函数) + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 7.3 比较函数 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较函数) + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 7.4 字符串处理函数 + +更多见文档[String Processing](./Operator-and-Expression.md#字符串函数) + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 7.5 数据类型转换函数 + +更多见文档[Data Type Conversion Function](./Operator-and-Expression.md#数据类型转换函数) + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 7.6 常序列生成函数 + +更多见文档[Constant Timeseries Generating Functions](./Operator-and-Expression.md#常序列生成函数) + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 7.7 选择函数 + +更多见文档[Selector Functions](./Operator-and-Expression.md#选择函数) + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 7.8 区间查询函数 + +更多见文档[Continuous Interval Functions](./Operator-and-Expression.md#区间查询函数) + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 7.9 趋势计算函数 + +更多见文档[Variation Trend Calculation Functions](./Operator-and-Expression.md#趋势计算函数) + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 7.10 采样函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#采样函数)。 +### 7.11 时间序列处理函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#时间序列处理函数)。 + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 7.12 时间序列处理函数 + +更多见文档[Time-Series](./Operator-and-Expression.md#时间序列处理函数) + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 8. 数据质量函数库 + +更多见文档[UDF-Libraries](../SQL-Manual/UDF-Libraries.md) + +### 8.1 数据质量 + +更多见文档[Data-Quality](../SQL-Manual/UDF-Libraries.md#数据质量) + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 8.2 数据画像 + +更多见文档[Data-Profiling](../SQL-Manual/UDF-Libraries.md#数据画像) + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 8.3 异常检测 + +更多见文档[Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#异常检测) + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 8.4 频域分析 + +更多见文档[Frequency-Domain](../SQL-Manual/UDF-Libraries.md#频域分析) + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 8.5 数据匹配 + +更多见文档[Data-Matching](../SQL-Manual/UDF-Libraries.md#数据匹配) + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 8.6 数据修复 + +更多见文档[Data-Repairing](../SQL-Manual/UDF-Libraries.md#数据修复) + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 8.7 序列发现 + +更多见文档[Series-Discovery](../SQL-Manual/UDF-Libraries.md#序列发现) + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 8.8 机器学习 + +更多见文档[Machine-Learning](../SQL-Manual/UDF-Libraries.md#机器学习) + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 9. Lambda 表达式 + +更多见文档[Lambda](./Operator-and-Expression.md#lambda-表达式) + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 10. 条件表达式 + +更多见文档[Conditional Expressions](./Operator-and-Expression.md#条件表达式) + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +#### 配置连续查询执行的周期性间隔 +```sql +CREATE CONTINUOUS QUERY cq1 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询的时间窗口大小 +``` +CREATE CONTINUOUS QUERY cq2 + +RESAMPLE RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 同时配置连续查询执行的周期性间隔和时间窗口大小 +```sql +CREATE CONTINUOUS QUERY cq3 + +RESAMPLE EVERY 20s RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询每次查询执行时间窗口的结束时间 +```sql +CREATE CONTINUOUS QUERY cq4 + +RESAMPLE EVERY 20s RANGE 40s, 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 没有GROUP BY TIME子句的连续查询 +```sql +CREATE CONTINUOUS QUERY cq5 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT temperature + 1 + + INTO root.precalculated_sg.::(temperature) + + FROM root.ln.*.* + + align by device + +END + + + +\> SELECT temperature from root.precalculated_sg.*.* align by device; +``` +### 11.2 连续查询的管理 + +#### 查询系统已有的连续查询 + +展示集群中所有的已注册的连续查询 +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` +```sql +SHOW CONTINUOUS QUERIES; +``` +#### 删除已有的连续查询 + +删除指定的名为cq_id的连续查询: + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` +```sql +DROP CONTINUOUS QUERY s1_count_cq; +``` +#### 作为子查询的替代品 + +1. 创建一个连续查询 +```sql +CREATE CQ s1_count_cq + +BEGIN + +​ SELECT count(s1) + +​ INTO root.sg_count.d.count_s1 + +​ FROM root.sg.d + +​ GROUP BY(30m) + +END +``` +1. 查询连续查询的结果 +```sql +SELECT avg(count_s1) from root.sg_count.d; +``` +## 12. 用户自定义函数 + +### 12.1 UDFParameters +```sql +SELECT UDF(s1, s2, 'key1'='iotdb', 'key2'='123.45') FROM root.sg.d; +``` +### 12.2 UDF 注册 + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +#### 不指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' +``` +#### 指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' USING URI 'http://jar/example.jar' +``` +### 12.3 UDF 卸载 + +```sql +DROP FUNCTION +``` +```sql +DROP FUNCTION example +``` +### 12.4 UDF 查询 + +#### 带自定义输入参数的查询 +```sql +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +``` +```sql +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; +``` +#### 与其他查询的嵌套查询 +```sql +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; + +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; + +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; + +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` +### 12.5 查看所有注册的 UDF +```sql +SHOW FUNCTIONS +``` +## 13. 权限管理 + +### 13.1 用户与角色相关 + +- 创建用户(需 MANAGE_USER 权限) + + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- 删除用户 (需 MANEGE_USER 权限) + + +```SQL +DROP USER +eg: DROP USER user1 +``` + +- 创建角色 (需 MANAGE_ROLE 权限) + +```SQL +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- 删除角色 (需 MANAGE_ROLE 权限) + + +```SQL +DROP ROLE +eg: DROP ROLE role1 +``` + +- 赋予用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- 移除用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- 列出所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER +``` + +- 列出所有角色 (需 MANAGE_ROLE 权限) + +```SQL +LIST ROLE +``` + +- 列出指定角色下所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- 列出指定用户下所有角色 + +用户可以列出自己的角色,但列出其他用户的角色需要拥有 MANAGE_ROLE 权限。 + +```SQL +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- 列出用户所有权限 + +用户可以列出自己的权限信息,但列出其他用户的权限需要拥有 MANAGE_USER 权限。 + +```SQL +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; + +``` + +- 列出角色所有权限 + +用户可以列出自己具有的角色的权限信息,列出其他角色的权限需要有 MANAGE_ROLE 权限。 + +```SQL +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- 更新密码 + +用户可以更新自己的密码,但更新其他用户密码需要具备MANAGE_USER 权限。 + +```SQL +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 13.2 授权与取消授权 + +用户使用授权语句对赋予其他用户权限,语法如下: + +```SQL +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +用户使用取消授权语句可以将其他的权限取消,语法如下: + +```SQL +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + diff --git a/src/zh/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md b/src/zh/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md index 825474b91..f439fa1e2 100644 --- a/src/zh/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md +++ b/src/zh/UserGuide/latest/Basic-Concept/Operate-Metadata_timecho.md @@ -339,239 +339,10 @@ It costs 0.058s + 数据库允许拥有的最大 DataRegionGroup 数量 -## 2. 设备模板管理 -IoTDB 支持设备模板功能,实现同类型不同实体的物理量元数据共享,减少元数据内存占用,同时简化同类型实体的管理。 +## 2. 时间序列管理 - -![img](/img/%E6%A8%A1%E6%9D%BF.png) - -![img](/img/template.jpg) - -### 2.1 创建设备模板 - -创建设备模板的 SQL 语法如下: - -```sql -CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' -``` - -**示例1:** 创建包含两个非对齐序列的元数据模板 - -```shell -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN ) -``` - -**示例2:** 创建包含一组对齐序列的元数据模板 - -```shell -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` - -其中,物理量 `lat` 和 `lon` 是对齐的。 - -创建模板时,系统会默认指定编码压缩方式,无需手动指定,若业务场景需要手动调整,可参考如下示例: -```shell -IoTDB> create device template t1 (temperature FLOAT encoding=RLE, status BOOLEAN encoding=PLAIN compression=SNAPPY) -``` -更多详细的数据类型与编码方式的对应列表请参见 [压缩&编码](../Technical-Insider/Encoding-and-Compression.md)。 - -### 2.2 挂载设备模板 - -元数据模板在创建后,需执行挂载操作,方可用于相应路径下的序列创建与数据写入。 - -**挂载模板前,需确保相关数据库已经创建。** - -**推荐将模板挂载在 database 节点上,不建议将模板挂载到 database 上层的节点上。** - -**模板挂载路径下禁止创建普通序列,已创建了普通序列的前缀路径上不允许挂载模板。** - -挂载元数据模板的 SQL 语句如下所示: - -```shell -IoTDB> set device template t1 to root.sg1.d1 -``` - -### 2.3 激活设备模板 - -挂载好设备模板后,且系统开启自动注册序列功能的情况下,即可直接进行数据的写入。例如 database 为 root.sg1,模板 t1 被挂载到了节点 root.sg1.d1,那么可直接向时间序列(如 root.sg1.d1.temperature 和 root.sg1.d1.status)写入时间序列数据,该时间序列已可被当作正常创建的序列使用。 - -**注意**:在插入数据之前或系统未开启自动注册序列功能,模板定义的时间序列不会被创建。可以使用如下SQL语句在插入数据前创建时间序列即激活模板: - -```shell -IoTDB> create timeseries using device template on root.sg1.d1 -``` - -**示例:** 执行以下语句 -```shell -IoTDB> set device template t1 to root.sg1.d1 -IoTDB> set device template t2 to root.sg1.d2 -IoTDB> create timeseries using device template on root.sg1.d1 -IoTDB> create timeseries using device template on root.sg1.d2 -``` - -查看此时的时间序列: -```sql -show timeseries root.sg1.** -``` - -```shell -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -| timeseries|alias| database|dataType|encoding|compression|tags|attributes|deadband|deadband parameters| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -|root.sg1.d1.temperature| null| root.sg1| FLOAT| RLE| SNAPPY|null| null| null| null| -| root.sg1.d1.status| null| root.sg1| BOOLEAN| PLAIN| SNAPPY|null| null| null| null| -| root.sg1.d2.lon| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -| root.sg1.d2.lat| null| root.sg1| FLOAT| GORILLA| SNAPPY|null| null| null| null| -+-----------------------+-----+-------------+--------+--------+-----------+----+----------+--------+-------------------+ -``` - -查看此时的设备: -```sql -show devices root.sg1.** -``` - -```shell -+---------------+---------+---------+ -| devices|isAligned| Template| -+---------------+---------+---------+ -| root.sg1.d1| false| null| -| root.sg1.d2| true| null| -+---------------+---------+---------+ -``` - -### 2.4 查看设备模板 - -- 查看所有设备模板 - -SQL 语句如下所示: - -```shell -IoTDB> show device templates -``` - -执行结果如下: -```shell -+-------------+ -|template name| -+-------------+ -| t2| -| t1| -+-------------+ -``` - -- 查看某个设备模板下的物理量 - -SQL 语句如下所示: - -```shell -IoTDB> show nodes in device template t1 -``` - -执行结果如下: -```shell -+-----------+--------+--------+-----------+ -|child nodes|dataType|encoding|compression| -+-----------+--------+--------+-----------+ -|temperature| FLOAT| RLE| SNAPPY| -| status| BOOLEAN| PLAIN| SNAPPY| -+-----------+--------+--------+-----------+ -``` - -- 查看挂载了某个设备模板的路径 - -```shell -IoTDB> show paths set device template t1 -``` - -执行结果如下: -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -- 查看使用了某个设备模板的路径(即模板在该路径上已激活,序列已创建) - -```shell -IoTDB> show paths using device template t1 -``` - -执行结果如下: -```shell -+-----------+ -|child paths| -+-----------+ -|root.sg1.d1| -+-----------+ -``` - -### 2.5 解除设备模板 - -若需删除模板表示的某一组时间序列,可采用解除模板操作,SQL语句如下所示: - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -``` - -或 - -```shell -IoTDB> deactivate device template t1 from root.sg1.d1 -``` - -解除操作支持批量处理,SQL语句如下所示: - -```shell -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -``` - -或 - -```shell -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` - -若解除命令不指定模板名称,则会将给定路径涉及的所有模板使用情况均解除。 - -### 2.6 卸载设备模板 - -卸载设备模板的 SQL 语句如下所示: - -```shell -IoTDB> unset device template t1 from root.sg1.d1 -``` - -**注意**:不支持卸载仍处于激活状态的模板,需保证执行卸载操作前解除对该模板的所有使用,即删除所有该模板表示的序列。 - -### 2.7 删除设备模板 - -删除设备模板的 SQL 语句如下所示: - -```shell -IoTDB> drop device template t1 -``` - -**注意**:不支持删除已经挂载的模板,需在删除操作前保证该模板卸载成功。 - -### 2.8 修改设备模板 - -在需要新增物理量的场景中,可以通过修改设备模板来给所有已激活该模板的设备新增物理量。 - -修改设备模板的 SQL 语句如下所示: - -```shell -IoTDB> alter device template t1 add (speed FLOAT, FLOAT TEXT) -``` - -**向已挂载模板的路径下的设备中写入数据,若写入请求中的物理量不在模板中,将自动扩展模板。** - - -## 3. 时间序列管理 - -### 3.1 创建时间序列 +### 2.1 创建时间序列 根据建立的数据模型,我们可以分别在两个数据库中创建相应的时间序列。创建时间序列的 SQL 语句如下所示: @@ -609,7 +380,7 @@ error: encoding TS_2DIFF does not support BOOLEAN 更多详细的数据类型与编码压缩方式的对应列表请参见 [压缩&编码](../Technical-Insider/Encoding-and-Compression.md)。 -### 3.2 创建对齐时间序列 +### 2.2 创建对齐时间序列 创建一组对齐时间序列的SQL语句如下所示: @@ -621,7 +392,7 @@ IoTDB> CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOA 对齐的时间序列也支持设置别名、标签、属性。 -### 3.3 删除时间序列 +### 2.3 删除时间序列 我们可以使用`(DELETE | DROP) TimeSeries `语句来删除我们之前创建的时间序列。SQL 语句如下所示: @@ -632,7 +403,7 @@ IoTDB> delete timeseries root.ln.wf02.* IoTDB> drop timeseries root.ln.wf02.* ``` -### 3.4 查看时间序列 +### 2.4 查看时间序列 * SHOW LATEST? TIMESERIES pathPattern? timeseriesWhereClause? limitClause? @@ -776,7 +547,7 @@ It costs 0.004s 需要注意的是,当查询路径不存在时,系统会返回 0 条时间序列。 -### 3.5 统计时间序列总数 +### 2.5 统计时间序列总数 IoTDB 支持使用`COUNT TIMESERIES`来统计一条路径中的时间序列个数。SQL 语句如下所示: @@ -863,7 +634,7 @@ It costs 0.002s > 注意:时间序列的路径只是过滤条件,与 level 的定义无关。 -### 3.6 活跃时间序列查询 +### 2.6 活跃时间序列查询 我们在原有的时间序列查询和统计上添加新的WHERE时间过滤条件,可以得到在指定时间范围中存在数据的时间序列。 需要注意的是, 在带有时间过滤的元数据查询中并不考虑视图的存在,只考虑TsFile中实际存储的时间序列。 @@ -904,7 +675,7 @@ IoTDB> count timeseries where time >= 15000 and time < 16000; ``` 关于活跃时间序列的定义,能通过正常查询查出来的数据就是活跃数据,也就是说插入但被删除的时间序列不在考虑范围内。 -### 3.7 标签点管理 +### 2.7 标签点管理 我们可以在创建时间序列的时候,为它添加别名和额外的标签和属性信息。 @@ -1067,9 +838,9 @@ IoTDB> show timeseries where TAGS(tag1)='v1' 上述对时间序列标签、属性的更新等操作都支持。 -## 4. 路径查询 +## 3. 路径查询 -### 4.1 路径(Path) +### 3.1 路径(Path) 路径(path)是用于表示时间序列的层级结构的表达式,其语法定义如下: ```SQL @@ -1087,7 +858,7 @@ IoTDB> show timeseries where TAGS(tag1)='v1' | '**' ; ``` -### 4.2 路径结点名(NodeName) +### 3.2 路径结点名(NodeName) - 路径中由 `.` 分割的部分称为路径结点名(nodeName)。 - 例如,`root.a.b.c` 是一个层级为 4 的路径,其中 root、a、b 和 c 都是路径结点名。 @@ -1102,11 +873,11 @@ IoTDB> show timeseries where TAGS(tag1)='v1' - UNICODE 中文字符(\u2E80 到 \u9FFF) - 大小写敏感性:在 Windows 系统上,数据库路径结点名是大小写不敏感的。例如,root.ln 和 root.LN 会被视为相同的路径。 -### 4.3 特殊字符(反引号) +### 3.3 特殊字符(反引号) 如果`路径结点名(NodeName)`中需要使用特殊字符(如空格、标点符号等),可以使用反引号(`)将结点名引用起来。更多关于反引号的使用方法,请参考[反引号](../SQL-Manual/Syntax-Rule.md#反引号)。 -### 4.4 路径模式(Path Pattern) +### 3.4 路径模式(Path Pattern) 为了使得在表达多个时间序列的时候更加方便快捷,IoTDB 为用户提供带通配符`*`或`**`的路径。通配符可以出现在路径中的任何层。 @@ -1119,7 +890,7 @@ IoTDB> show timeseries where TAGS(tag1)='v1' **注意**:* 和 ** 不能放在路径的开头。 -### 4.5 查看路径的所有子路径 +### 3.5 查看路径的所有子路径 ``` SHOW CHILD PATHS pathPattern @@ -1155,7 +926,7 @@ It costs 0.002s +---------------+ ``` -### 4.6 查看路径的下一级节点 +### 3.6 查看路径的下一级节点 ``` SHOW CHILD NODES pathPattern @@ -1186,7 +957,7 @@ SHOW CHILD NODES pathPattern +------------+ ``` -### 4.7 统计节点数 +### 3.7 统计节点数 IoTDB 支持使用`COUNT NODES LEVEL=`来统计当前 Metadata 树下满足某路径模式的路径中指定层级的节点个数。这条语句可以用来统计带有特定采样点的设备数。例如: @@ -1236,7 +1007,7 @@ It costs 0.002s > 注意:时间序列的路径只是过滤条件,与 level 的定义无关。 -### 4.8 查看设备 +### 3.8 查看设备 * SHOW DEVICES pathPattern? (WITH DATABASE)? devicesWhereClause? limitClause? @@ -1348,7 +1119,7 @@ Total line number = 2 It costs 0.001s ``` -### 4.9 统计设备数量 +### 3.9 统计设备数量 * COUNT DEVICES \ @@ -1393,7 +1164,7 @@ Total line number = 1 It costs 0.004s ``` -### 4.10 活跃设备查询 +### 3.10 活跃设备查询 和活跃时间序列一样,我们可以在查看和统计设备的基础上添加时间过滤条件来查询在某段时间内存在数据的活跃设备。这里活跃的定义与活跃时间序列相同,使用样例如下: ``` IoTDB> insert into root.sg.data(timestamp, s1,s2) values(15000, 1, 2); diff --git a/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual.md b/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual.md index 1b35aacd8..ab81bf055 100644 --- a/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual.md +++ b/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual.md @@ -1,1973 +1,23 @@ -# SQL手册 - -## 1. 元数据操作 - -### 1.1 数据库管理 - -#### 创建数据库 - -```sql -CREATE DATABASE root.ln -``` - -#### 查看数据库 - -```sql -show databases -show databases root.* -show databases root.** -``` - -#### 删除数据库 - -```sql -DELETE DATABASE root.ln -DELETE DATABASE root.sgcc -DELETE DATABASE root.** -``` - -#### 统计数据库数量 - -```sql -count databases -count databases root.* -count databases root.sgcc.* -count databases root.sgcc -``` - -### 1.2 时间序列管理 - -#### 创建时间序列 - -```sql -create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN -create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT -create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT -create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN -create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN -create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT -``` - -- 简化版 - -```sql -create timeseries root.ln.wf01.wt01.status BOOLEAN -create timeseries root.ln.wf01.wt01.temperature FLOAT -create timeseries root.ln.wf02.wt02.hardware TEXT -create timeseries root.ln.wf02.wt02.status BOOLEAN -create timeseries root.sgcc.wf03.wt01.status BOOLEAN -create timeseries root.sgcc.wf03.wt01.temperature FLOAT -``` - -- 错误提示 - -```sql -create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN -> error: encoding TS_2DIFF does not support BOOLEAN -``` - -#### 创建对齐时间序列 - -```sql -CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOAT) -``` - -#### 删除时间序列 - -```sql -delete timeseries root.ln.wf01.wt01.status -delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware -delete timeseries root.ln.wf02.* -drop timeseries root.ln.wf02.* -``` - -#### 查看时间序列 - -```sql -SHOW TIMESERIES -SHOW TIMESERIES -SHOW TIMESERIES root.** -SHOW TIMESERIES root.ln.** -SHOW TIMESERIES root.ln.** limit 10 offset 10 -SHOW TIMESERIES root.ln.** where timeseries contains 'wf01.wt' -SHOW TIMESERIES root.ln.** where dataType=FLOAT -SHOW TIMESERIES root.ln.** where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -SHOW LATEST TIMESERIES -``` - -#### 统计时间序列数量 - -```sql -COUNT TIMESERIES root.** -COUNT TIMESERIES root.ln.** -COUNT TIMESERIES root.ln.*.*.status -COUNT TIMESERIES root.ln.wf01.wt01.status -COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' -COUNT TIMESERIES root.** WHERE DATATYPE = INT64 -COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' -COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' -COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 -COUNT TIMESERIES root.** WHERE time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -COUNT TIMESERIES root.** GROUP BY LEVEL=1 -COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 -COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 -``` - -#### 标签点管理 - -```sql -create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) -``` - -- 重命名标签或属性 - -```sql -ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 -``` - -- 重新设置标签或属性的值 - -```sql -ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 -``` - -- 删除已经存在的标签或属性 - -```sql -ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 -``` - -- 添加新的标签 - -```sql -ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 -``` - -- 添加新的属性 - -```sql -ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 -``` - -- 更新插入别名,标签和属性 - -```sql -ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4) -``` - -- 使用标签作为过滤条件查询时间序列 - -```sql -SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -``` - -返回给定路径的下的所有满足条件的时间序列信息: - -```sql -ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c -ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 -show timeseries root.ln.** where TAGS(unit)='c' -show timeseries root.ln.** where TAGS(description) contains 'test1' -``` - -- 使用标签作为过滤条件统计时间序列数量 - -```sql -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause -COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= -``` - -返回给定路径的下的所有满足条件的时间序列的数量: - -```sql -count timeseries -count timeseries root.** where TAGS(unit)='c' -count timeseries root.** where TAGS(unit)='c' group by level = 2 -``` - -创建对齐时间序列: - -```sql -create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) -``` - -支持查询: - -```sql -show timeseries where TAGS(tag1)='v1' -``` - -### 1.3 时间序列路径管理 - -#### 查看路径的所有子路径 - -```sql -SHOW CHILD PATHS pathPattern -- 查询 root.ln 的下一层:show child paths root.ln -- 查询形如 root.xx.xx.xx 的路径:show child paths root.*.* -``` -#### 查看路径的所有子节点 - -```sql -SHOW CHILD NODES pathPattern - -- 查询 root 的下一层:show child nodes root -- 查询 root.ln 的下一层 :show child nodes root.ln -``` -#### 查看设备 - -```sql -IoTDB> show devices - -IoTDB> show devices root.ln.** - -IoTDB> show devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -``` -##### 查看设备及其 database 信息 - -```sql -IoTDB> show devices with database - -IoTDB> show devices root.ln.** with database -``` -#### 统计节点数 - -```sql -IoTDB > COUNT NODES root.** LEVEL=2 - -IoTDB > COUNT NODES root.ln.** LEVEL=2 - -IoTDB > COUNT NODES root.ln.wf01.* LEVEL=3 - -IoTDB > COUNT NODES root.**.temperature LEVEL=3 -``` -#### 统计设备数量 - -```sql - -IoTDB> count devices - -IoTDB> count devices root.ln.** - -IoTDB> count devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -``` -### 1.4 设备模板管理 - - -![img](/img/%E6%A8%A1%E6%9D%BF.png) -![img](/img/template.jpg) - - - -#### 创建设备模板 - -```Go -CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' -``` - -创建包含两个非对齐序列的设备模板 -```sql -IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) -``` -创建包含一组对齐序列的设备模板 -```sql -IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) -``` -#### 挂载设备模板 -```sql -IoTDB> set DEVICE TEMPLATE t1 to root.sg1 -``` -#### 激活设备模板 -```sql -IoTDB> create timeseries using DEVICE TEMPLATE on root.sg1.d1 - -IoTDB> set DEVICE TEMPLATE t1 to root.sg1.d1 - -IoTDB> set DEVICE TEMPLATE t2 to root.sg1.d2 - -IoTDB> create timeseries using device template on root.sg1.d1 - -IoTDB> create timeseries using device template on root.sg1.d2 -``` -#### 查看设备模板 -```sql -IoTDB> show device templates -``` -- 查看某个设备模板下的物理量 -```sql -IoTDB> show nodes in device template t1 -``` -- 查看挂载了某个设备模板的路径 -```sql -IoTDB> show paths set device template t1 -``` -- 查看使用了某个设备模板的路径(即模板在该路径上已激活,序列已创建) -```sql -IoTDB> show paths using device template t1 -``` -#### 解除设备模板 -```sql -IoTDB> delete timeseries of device template t1 from root.sg1.d1 -``` -```sql -IoTDB> deactivate device template t1 from root.sg1.d1 -``` -批量处理 -```sql -IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* -``` -```sql -IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* -``` -#### 卸载设备模板 -```sql -IoTDB> unset device template t1 from root.sg1.d1 -``` -#### 删除设备模板 -```sql -IoTDB> drop device template t1 -``` -### 1.5 数据存活时间管理 - -#### 设置 TTL -```sql -IoTDB> set ttl to root.ln 3600000 -``` -```sql -IoTDB> set ttl to root.sgcc.** 3600000 -``` -```sql -IoTDB> set ttl to root.** 3600000 -``` -#### 取消 TTL -```sql -IoTDB> unset ttl from root.ln -``` -```sql -IoTDB> unset ttl from root.sgcc.** -``` -```sql -IoTDB> unset ttl from root.** -``` - -#### 显示 TTL -```sql -IoTDB> SHOW ALL TTL -``` -```sql -IoTDB> SHOW TTL ON pathPattern -``` -```sql -IoTDB> show DEVICES -``` -## 2. 写入数据 - -### 2.1 写入单列数据 -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) -``` -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1'),(2, 'v1') -``` -### 2.2 写入多列数据 -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) values (2, false, 'v2') -``` -```sql -IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') -``` -### 2.3 使用服务器时间戳 -```sql -IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') -``` -### 2.4 写入对齐时间序列数据 -```sql -IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) -``` -```sql -IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(1, 1, 1) -``` -```sql -IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(2, 2, 2), (3, 3, 3) -``` -```sql -IoTDB > select * from root.sg1.d1 -``` -### 2.5 加载 TsFile 文件数据 - -load '' [sglevel=int][onSuccess=delete/none] - -#### 通过指定文件路径(绝对路径)加载单 tsfile 文件 - -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` -- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` - - -#### 通过指定文件夹路径(绝对路径)批量加载文件 - -- `load '/Users/Desktop/data'` -- `load '/Users/Desktop/data' sglevel=1` -- `load '/Users/Desktop/data' onSuccess=delete` -- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` - -## 3. 删除数据 - -### 3.1 删除单列数据 -```sql -delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; -``` -```sql -delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; -``` -```sql -delete from root.ln.wf02.wt02.status where time < 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time <= 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time < 20 and time > 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 -``` -```sql -delete from root.ln.wf02.wt02.status where time > 20 -``` -```sql -delete from root.ln.wf02.wt02.status where time >= 20 -``` -```sql -delete from root.ln.wf02.wt02.status where time = 20 -``` -出错: -```sql -delete from root.ln.wf02.wt02.status where time > 4 or time < 0 - -Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic - -expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' -``` - -删除时间序列中的所有数据: -```sql -delete from root.ln.wf02.wt02.status -``` -### 3.2 删除多列数据 -```sql -delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; -``` -声明式的编程方式: -```sql -IoTDB> delete from root.ln.wf03.wt02.status where time < now() - -Msg: The statement is executed successfully. -``` -## 4. 数据查询 - -### 4.1 基础查询 - -#### 时间过滤查询 -```sql -select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 -``` -#### 根据一个时间区间选择多列数据 -```sql -select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` -#### 按照多个时间区间选择同一设备的多列数据 -```sql -select status, temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` -#### 按照多个时间区间选择不同设备的多列数据 -```sql -select wf01.wt01.status, wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); -``` -#### 根据时间降序返回结果集 -```sql -select * from root.ln.** where time > 1 order by time desc limit 10; -``` -### 4.2 选择表达式 - -#### 使用别名 -```sql -select s1 as temperature, s2 as speed from root.ln.wf01.wt01; -``` -#### 运算符 - -#### 函数 - -不支持: -```sql -select s1, count(s1) from root.sg.d1; - -select sin(s1), count(s1) from root.sg.d1; - -select s1, count(s1) from root.sg.d1 group by ([10,100),10ms); -``` -##### 时间序列查询嵌套表达式 - -示例 1: -```sql -select a, - -​ b, - -​ ((a + 1) * 2 - 1) % 2 + 1.5, - -​ sin(a + sin(a + sin(b))), - -​ -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 - -from root.sg1; -``` -示例 2: -```sql -select (a + b) * 2 + sin(a) from root.sg - -示例 3: - -select (a + *) / 2 from root.sg1 - -示例 4: - -select (a + b) * 3 from root.sg, root.ln -``` -##### 聚合查询嵌套表达式 - -示例 1: -```sql -select avg(temperature), - -​ sin(avg(temperature)), - -​ avg(temperature) + 1, - -​ -sum(hardware), - -​ avg(temperature) + sum(hardware) - -from root.ln.wf01.wt01; -``` -示例 2: -```sql -select avg(*), - -​ (avg(*) + 1) * 3 / 2 -1 - -from root.sg1 -``` -示例 3: -```sql -select avg(temperature), - -​ sin(avg(temperature)), - -​ avg(temperature) + 1, - -​ -sum(hardware), - -​ avg(temperature) + sum(hardware) as custom_sum - -from root.ln.wf01.wt01 - -GROUP BY([10, 90), 10ms); -``` -#### 最新点查询 - -SQL 语法: - -```Go -select last [COMMA ]* from < PrefixPath > [COMMA < PrefixPath >]* [ORDER BY TIMESERIES (DESC | ASC)?] -``` - -查询 root.ln.wf01.wt01.status 的最新数据点 -```sql -IoTDB> select last status from root.ln.wf01.wt01 -``` -查询 root.ln.wf01.wt01 下 status,temperature 时间戳大于等于 2017-11-07T23:50:00 的最新数据点 -```sql -IoTDB> select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 -``` - 查询 root.ln.wf01.wt01 下所有序列的最新数据点,并按照序列名降序排列 -```sql -IoTDB> select last * from root.ln.wf01.wt01 order by timeseries desc; -``` -### 4.3 查询过滤条件 - -#### 时间过滤条件 - -选择时间戳大于 2022-01-01T00:05:00.000 的数据: -```sql -select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; -``` -选择时间戳等于 2022-01-01T00:05:00.000 的数据: -```sql -select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; -``` -选择时间区间 [2017-11-01T00:05:00.000, 2017-11-01T00:12:00.000) 内的数据: -```sql -select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; -``` -#### 值过滤条件 - -选择值大于 36.5 的数据: -```sql -select temperature from root.sg1.d1 where temperature > 36.5; -``` -选择值等于 true 的数据: -```sql -select status from root.sg1.d1 where status = true; -``` -选择区间 [36.5,40] 内或之外的数据: -```sql -select temperature from root.sg1.d1 where temperature between 36.5 and 40; -``` -```sql -select temperature from root.sg1.d1 where temperature not between 36.5 and 40; -``` -选择值在特定范围内的数据: -```sql -select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -``` -选择值在特定范围外的数据: -```sql -select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -``` -选择值为空的数据: -```sql -select code from root.sg1.d1 where temperature is null; -``` -选择值为非空的数据: -```sql -select code from root.sg1.d1 where temperature is not null; -``` -#### 模糊查询 - -查询 `root.sg.d1` 下 `value` 含有`'cc'`的数据 -```sql -IoTDB> select * from root.sg.d1 where value like '%cc%' -``` -查询 `root.sg.d1` 下 `value` 中间为 `'b'`、前后为任意单个字符的数据 -```sql -IoTDB> select * from root.sg.device where value like '_b_' -``` -查询 root.sg.d1 下 value 值为26个英文字符组成的字符串 -```sql -IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -``` - -查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的 -```sql -IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -``` - -### 4.4 分段分组聚合 - -#### 未指定滑动步长的时间区间分组聚合查询 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); -``` -#### 指定滑动步长的时间区间分组聚合查询 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); -``` -滑动步长可以小于聚合窗口 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-01 10:00:00), 4h, 2h); -``` -#### 按照自然月份的时间区间分组聚合查询 -```sql -select count(status) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -``` -每个时间间隔窗口内都有数据 -```sql -select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); -``` -#### 左开右闭区间 -```sql -select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); -``` -#### 与分组聚合混合使用 - -统计降采样后的数据点个数 -```sql -select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; -``` -加上滑动 Step 的降采样后的结果也可以汇总 -```sql -select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; -``` -#### 路径层级分组聚合 - -统计不同 database 下 status 序列的数据点个数 -```sql -select count(status) from root.** group by level = 1 -``` - 统计不同设备下 status 序列的数据点个数 -```sql -select count(status) from root.** group by level = 3 -``` -统计不同 database 下的不同设备中 status 序列的数据点个数 -```sql -select count(status) from root.** group by level = 1, 3 -``` -查询所有序列下温度传感器 temperature 的最大值 -```sql -select max_value(temperature) from root.** group by level = 0 -``` -查询某一层级下所有传感器拥有的总数据点数 -```sql -select count(*) from root.ln.** group by level = 2 -``` -#### 标签分组聚合 - -##### 单标签聚合查询 -```sql -SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); -``` -##### 多标签聚合查询 -```sql -SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); -``` -##### 基于时间区间的标签聚合查询 -```sql -SELECT AVG(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); -``` -#### 差值分段聚合 -```sql -group by variation(controlExpression[,delta][,ignoreNull=true/false]) -``` -##### delta=0时的等值事件分段 -```sql -select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) -``` -指定ignoreNull为false -```sql -select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) -``` -##### delta!=0时的差值事件分段 -```sql -select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) -``` -#### 条件分段聚合 -```sql -group by condition(predict,[keep>/>=/=/<=/<]threshold,[,ignoreNull=true/false]) -``` -查询至少连续两行以上的charging_status=1的数据 -```sql -select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=true) -``` -当设置`ignoreNull`为false时,遇到null值为将其视为一个不满足条件的行,得到结果原先的分组被含null的行拆分 -```sql -select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=false) -``` -#### 会话分段聚合 -```sql -group by session(timeInterval) -``` -按照不同的时间单位设定时间间隔 -```sql -select __endTime,count(*) from root.** group by session(1d) -``` -和`HAVING`、`ALIGN BY DEVICE`共同使用 -```sql -select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device -``` -#### 点数分段聚合 -```sql -group by count(controlExpression, size[,ignoreNull=true/false]) -``` -select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) - -当使用ignoreNull将null值也考虑进来 -```sql -select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) -``` -### 4.5 聚合结果过滤 - -不正确的: -```sql -select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 - -select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 - -select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 - -select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 -``` -SQL 示例: -```sql - select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 2; - - select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device; -``` -### 4.6 结果集补空值 -```sql -FILL '(' PREVIOUS | LINEAR | constant (, interval=DURATION_LITERAL)? ')' -``` -#### `PREVIOUS` 填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); -``` -#### `PREVIOUS` 填充并指定填充超时阈值 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); -``` -#### `LINEAR` 填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); -``` -#### 常量填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); -``` -使用 `BOOLEAN` 类型的常量填充 -```sql -select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); -``` -### 4.7 查询结果分页 - -#### 按行分页 - - 基本的 `LIMIT` 子句 -```sql -select status, temperature from root.ln.wf01.wt01 limit 10 -``` -带 `OFFSET` 的 `LIMIT` 子句 -```sql -select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 -``` -`LIMIT` 子句与 `WHERE` 子句结合 -```sql -select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 -``` - `LIMIT` 子句与 `GROUP BY` 子句组合 -```sql -select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 -``` -#### 按列分页 - - 基本的 `SLIMIT` 子句 -```sql -select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 -``` -带 `SOFFSET` 的 `SLIMIT` 子句 -```sql -select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 -``` -`SLIMIT` 子句与 `GROUP BY` 子句结合 -```sql -select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 -``` -`SLIMIT` 子句与 `LIMIT` 子句结合 -```sql -select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 -``` -### 4.8 排序 - -时间对齐模式下的排序 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; -``` -设备对齐模式下的排序 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; -``` -在时间戳相等时按照设备名排序 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; -``` -没有显式指定时 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -``` -对聚合后的结果进行排序 -```sql -select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device -``` -### 4.9 查询对齐模式 - -#### 按设备对齐 -```sql -select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; -``` -### 4.10 查询写回(SELECT INTO) - -#### 整体描述 -```sql -selectIntoStatement - -​ : SELECT - -​ resultColumn [, resultColumn] ... - -​ INTO intoItem [, intoItem] ... - -​ FROM prefixPath [, prefixPath] ... - -​ [WHERE whereCondition] - -​ [GROUP BY groupByTimeClause, groupByLevelClause] - -​ [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] - -​ [LIMIT rowLimit OFFSET rowOffset] - -​ [ALIGN BY DEVICE] - -​ ; - - - -intoItem - -​ : [ALIGNED] intoDevicePath '(' intoMeasurementName [',' intoMeasurementName]* ')' - -​ ; -``` -按时间对齐,将 `root.sg` database 下四条序列的查询结果写入到 `root.sg_copy` database 下指定的四条序列中 -```sql -IoTDB> select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; -``` -按时间对齐,将聚合查询的结果存储到指定序列中 -```sql -IoTDB> select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); -``` -按设备对齐 -```sql -IoTDB> select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -``` -按设备对齐,将表达式计算的结果存储到指定序列中 -```sql -IoTDB> select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; -``` -#### 使用变量占位符 - -##### 按时间对齐(默认) - -###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 -``` - -select s1, s2 - -into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) - -from root.sg.d1, root.sg.d2; -``` - -该语句等价于: -``` - -select s1, s2 - -into root.sg_copy.d1(s1), root.sg_copy.d2(s1), root.sg_copy.d1(s2), root.sg_copy.d2(s2) - -from root.sg.d1, root.sg.d2; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 - -``` -select d1.s1, d1.s2, d2.s3, d3.s4 - -into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) - -from root.sg; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 - -``` -select * into root.sg_bk.::(::) from root.sg.**; -``` - -##### 按设备对齐(使用 `ALIGN BY DEVICE`) - -###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 -``` - -select s1, s2, s3, s4 - -into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) - -from root.sg.d1, root.sg.d2, root.sg.d3 - -align by device; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 -``` - -select avg(s1), sum(s2) + sum(s3), count(s4) - -into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) - -from root.** - -align by device; -``` - -###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 -``` - -select * into ::(backup_${4}) from root.sg.** align by device; -``` - -#### 指定目标序列为对齐序列 -``` - -select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; -``` -## 5. 运维语句 -生成对应的查询计划 -``` -explain select s1,s2 from root.sg.d1 -``` -执行对应的查询语句,并获取分析结果 -``` -explain analyze select s1,s2 from root.sg.d1 order by s1 -``` -## 6. 运算符 - -更多见文档[Operator-and-Expression](./Operator-and-Expression.md) - -### 6.1 算数运算符 - -更多见文档 [Arithmetic Operators and Functions](./Operator-and-Expression.md#算数运算符) - -```sql -select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 -``` - -### 6.2 比较运算符 - -更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较运算符) - -```sql -# Basic comparison operators -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; - -# `BETWEEN ... AND ...` operator -select temperature from root.sg1.d1 where temperature between 36.5 and 40; -select temperature from root.sg1.d1 where temperature not between 36.5 and 40; - -# Fuzzy matching operator: Use `Like` for fuzzy matching -select * from root.sg.d1 where value like '%cc%' -select * from root.sg.device where value like '_b_' - -# Fuzzy matching operator: Use `Regexp` for fuzzy matching -select * from root.sg.d1 where value regexp '^[A-Za-z]+$' -select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 -select b, b like '1%', b regexp '[0-2]' from root.test; - -# `IS NULL` operator -select code from root.sg1.d1 where temperature is null; -select code from root.sg1.d1 where temperature is not null; - -# `IN` operator -select code from root.sg1.d1 where code in ('200', '300', '400', '500'); -select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); -select a, a in (1, 2) from root.test; -``` - -### 6.3 逻辑运算符 - -更多见文档[Logical Operators](./Operator-and-Expression.md#逻辑运算符) - -```sql -select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; -``` - -## 7. 内置函数 - -更多见文档[Operator-and-Expression](./Operator-and-Expression.md#聚合函数) - -### 7.1 Aggregate Functions - -更多见文档[Aggregate Functions](./Operator-and-Expression.md#聚合函数) - -```sql -select count(status) from root.ln.wf01.wt01; - -select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; -select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; - -select time_duration(s1) from root.db.d1; -``` - -### 7.2 算数函数 - -更多见文档[Arithmetic Operators and Functions](./Operator-and-Expression.md#数学函数) - -```sql -select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; -select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; -``` - -### 7.3 比较函数 - -更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较函数) - -```sql -select ts, on_off(ts, 'threshold'='2') from root.test; -select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; -``` - -### 7.4 字符串处理函数 - -更多见文档[String Processing](./Operator-and-Expression.md#字符串函数) - -```sql -select s1, string_contains(s1, 's'='warn') from root.sg1.d4; -select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; -select s1, length(s1) from root.sg1.d1 -select s1, locate(s1, "target"="1") from root.sg1.d1 -select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 -select s1, startswith(s1, "target"="1") from root.sg1.d1 -select s1, endswith(s1, "target"="1") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 -select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 -select s1, substring(s1 from 1 for 2) from root.sg1.d1 -select s1, replace(s1, 'es', 'tt') from root.sg1.d1 -select s1, upper(s1) from root.sg1.d1 -select s1, lower(s1) from root.sg1.d1 -select s3, trim(s3) from root.sg1.d1 -select s1, s2, strcmp(s1, s2) from root.sg1.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 -select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 -select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 -select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 -select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 -``` - -### 7.5 数据类型转换函数 - -更多见文档[Data Type Conversion Function](./Operator-and-Expression.md#数据类型转换函数) - -```sql -SELECT cast(s1 as INT32) from root.sg -``` - -### 7.6 常序列生成函数 - -更多见文档[Constant Timeseries Generating Functions](./Operator-and-Expression.md#常序列生成函数) - -```sql -select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; -``` - -### 7.7 选择函数 - -更多见文档[Selector Functions](./Operator-and-Expression.md#选择函数) - -```sql -select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; -``` - -### 7.8 区间查询函数 - -更多见文档[Continuous Interval Functions](./Operator-and-Expression.md#区间查询函数) - -```sql -select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; -``` - -### 7.9 趋势计算函数 - -更多见文档[Variation Trend Calculation Functions](./Operator-and-Expression.md#趋势计算函数) - -```sql -select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; - -SELECT DIFF(s1), DIFF(s2) from root.test; -SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; -``` - -### 7.10 采样函数 - -更多见文档[Sample Functions](./Operator-and-Expression.md#采样函数)。 -### 7.11 时间序列处理函数 - -更多见文档[Sample Functions](./Operator-and-Expression.md#时间序列处理函数)。 - -```sql -select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; -select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; -select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; -select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; - -select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 -select M4(s1,'windowSize'='10') from root.vehicle.d1 -``` - -### 7.12 时间序列处理函数 - -更多见文档[Time-Series](./Operator-and-Expression.md#时间序列处理函数) - -```sql -select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 -``` - -## 8. 数据质量函数库 - -更多见文档[UDF-Libraries](../SQL-Manual/UDF-Libraries.md) - -### 8.1 数据质量 - -更多见文档[Data-Quality](../SQL-Manual/UDF-Libraries.md#数据质量) - -```sql -# Completeness -select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Consistency -select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Timeliness -select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Validity -select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 -select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 - -# Accuracy -select Accuracy(t1,t2,t3,m1,m2,m3) from root.test -``` - -### 8.2 数据画像 - -更多见文档[Data-Profiling](../SQL-Manual/UDF-Libraries.md#数据画像) - -```sql -# ACF -select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 - -# Distinct -select distinct(s2) from root.test.d2 - -# Histogram -select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 - -# Integral -select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 -select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 - -# IntegralAvg -select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 - -# Mad -select mad(s0) from root.test -select mad(s0, "error"="0.01") from root.test - -# Median -select median(s0, "error"="0.01") from root.test - -# MinMax -select minmax(s1) from root.test - -# Mode -select mode(s2) from root.test.d2 - -# MvAvg -select mvavg(s1, "window"="3") from root.test - -# PACF -select pacf(s1, "lag"="5") from root.test - -# Percentile -select percentile(s0, "rank"="0.2", "error"="0.01") from root.test - -# Quantile -select quantile(s0, "rank"="0.2", "K"="800") from root.test - -# Period -select period(s1) from root.test.d3 - -# QLB -select QLB(s1) from root.test.d1 - -# Resample -select resample(s1,'every'='5m','interp'='linear') from root.test.d1 -select resample(s1,'every'='30m','aggr'='first') from root.test.d1 -select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 - -# Sample -select sample(s1,'method'='reservoir','k'='5') from root.test.d1 -select sample(s1,'method'='isometric','k'='5') from root.test.d1 - -# Segment -select segment(s1, "error"="0.1") from root.test - -# Skew -select skew(s1) from root.test.d1 - -# Spline -select spline(s1, "points"="151") from root.test - -# Spread -select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 - -# Stddev -select stddev(s1) from root.test.d1 - -# ZScore -select zscore(s1) from root.test -``` - -### 8.3 异常检测 - -更多见文档[Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#异常检测) - -```sql -# IQR -select iqr(s1) from root.test - -# KSigma -select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# LOF -select lof(s1,s2) from root.test.d1 where time<1000 -select lof(s1, "method"="series") from root.test.d1 where time<1000 - -# MissDetect -select missdetect(s2,'minlen'='10') from root.test.d2 - -# Range -select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 - -# TwoSidedFilter -select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test - -# Outlier -select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test - -# MasterTrain -select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test - -# MasterDetect -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test -select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test -``` - -### 8.4 频域分析 - -更多见文档[Frequency-Domain](../SQL-Manual/UDF-Libraries.md#频域分析) - -```sql -# Conv -select conv(s1,s2) from root.test.d2 - -# Deconv -select deconv(s3,s2) from root.test.d2 -select deconv(s3,s2,'result'='remainder') from root.test.d2 - -# DWT -select dwt(s1,"method"="haar") from root.test.d1 - -# FFT -select fft(s1) from root.test.d1 -select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 - -# HighPass -select highpass(s1,'wpass'='0.45') from root.test.d1 - -# IFFT -select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 - -# LowPass -select lowpass(s1,'wpass'='0.45') from root.test.d1 - -# Envelope -select envelope(s1) from root.test.d1 -``` - -### 8.5 数据匹配 - -更多见文档[Data-Matching](../SQL-Manual/UDF-Libraries.md#数据匹配) - -```sql -# Cov -select cov(s1,s2) from root.test.d2 - -# DTW -select dtw(s1,s2) from root.test.d2 - -# Pearson -select pearson(s1,s2) from root.test.d2 - -# PtnSym -select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 - -# XCorr -select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 -``` - -### 8.6 数据修复 - -更多见文档[Data-Repairing](../SQL-Manual/UDF-Libraries.md#数据修复) - -```sql -# TimestampRepair -select timestamprepair(s1,'interval'='10000') from root.test.d2 -select timestamprepair(s1) from root.test.d2 - -# ValueFill -select valuefill(s1) from root.test.d2 -select valuefill(s1,"method"="previous") from root.test.d2 - -# ValueRepair -select valuerepair(s1) from root.test.d2 -select valuerepair(s1,'method'='LsGreedy') from root.test.d2 - -# MasterRepair -select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test - -# SeasonalRepair -select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 -select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 -``` - -### 8.7 序列发现 - -更多见文档[Series-Discovery](../SQL-Manual/UDF-Libraries.md#序列发现) - -```sql -# ConsecutiveSequences -select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 -select consecutivesequences(s1,s2) from root.test.d1 - -# ConsecutiveWindows -select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 -``` - -### 8.8 机器学习 - -更多见文档[Machine-Learning](../SQL-Manual/UDF-Libraries.md#机器学习) - -```sql -# AR -select ar(s0,"p"="2") from root.test.d0 - -# Representation -select representation(s0,"tb"="3","vb"="2") from root.test.d0 - -# RM -select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 -``` - -## 9. Lambda 表达式 - -更多见文档[Lambda](./Operator-and-Expression.md#lambda-表达式) - -```sql -select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` -``` - -## 10. 条件表达式 - -更多见文档[Conditional Expressions](./Operator-and-Expression.md#条件表达式) - -```sql -select T, P, case -when 1000=1050 then "bad temperature" -when P<=1000000 or P>=1100000 then "bad pressure" -end as `result` -from root.test1 - -select str, case -when str like "%cc%" then "has cc" -when str like "%dd%" then "has dd" -else "no cc and dd" end as `result` -from root.test2 - -select -count(case when x<=1 then 1 end) as `(-∞,1]`, -count(case when 1 -[RESAMPLE - [EVERY ] - [BOUNDARY ] - [RANGE [, end_time_offset]] -] -[TIMEOUT POLICY BLOCKED|DISCARD] -BEGIN - SELECT CLAUSE - INTO CLAUSE - FROM CLAUSE - [WHERE CLAUSE] - [GROUP BY([, ]) [, level = ]] - [HAVING CLAUSE] - [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] - [LIMIT rowLimit OFFSET rowOffset] - [ALIGN BY DEVICE] -END -``` - -#### 配置连续查询执行的周期性间隔 -```sql -CREATE CONTINUOUS QUERY cq1 - -RESAMPLE EVERY 20s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - -END - - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 配置连续查询的时间窗口大小 -``` -CREATE CONTINUOUS QUERY cq2 - -RESAMPLE RANGE 40s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - -END - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 同时配置连续查询执行的周期性间隔和时间窗口大小 -```sql -CREATE CONTINUOUS QUERY cq3 - -RESAMPLE EVERY 20s RANGE 40s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - - FILL(100.0) - -END - - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 配置连续查询每次查询执行时间窗口的结束时间 -```sql -CREATE CONTINUOUS QUERY cq4 - -RESAMPLE EVERY 20s RANGE 40s, 20s - -BEGIN - - SELECT max_value(temperature) - - INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) - - FROM root.ln.*.* - - GROUP BY(10s) - - FILL(100.0) - -END - - - -\> SELECT temperature_max from root.ln.*.*; -``` -#### 没有GROUP BY TIME子句的连续查询 -```sql -CREATE CONTINUOUS QUERY cq5 - -RESAMPLE EVERY 20s - -BEGIN - - SELECT temperature + 1 - - INTO root.precalculated_sg.::(temperature) - - FROM root.ln.*.* - - align by device - -END - - - -\> SELECT temperature from root.precalculated_sg.*.* align by device; -``` -### 11.2 连续查询的管理 - -#### 查询系统已有的连续查询 - -展示集群中所有的已注册的连续查询 -```sql -SHOW (CONTINUOUS QUERIES | CQS) -``` -```sql -SHOW CONTINUOUS QUERIES; -``` -#### 删除已有的连续查询 - -删除指定的名为cq_id的连续查询: - -```sql -DROP (CONTINUOUS QUERY | CQ) -``` -```sql -DROP CONTINUOUS QUERY s1_count_cq; -``` -#### 作为子查询的替代品 - -1. 创建一个连续查询 -```sql -CREATE CQ s1_count_cq - -BEGIN - -​ SELECT count(s1) - -​ INTO root.sg_count.d.count_s1 - -​ FROM root.sg.d - -​ GROUP BY(30m) - -END -``` -1. 查询连续查询的结果 -```sql -SELECT avg(count_s1) from root.sg_count.d; -``` -## 12. 用户自定义函数 - -### 12.1 UDFParameters -```sql -SELECT UDF(s1, s2, 'key1'='iotdb', 'key2'='123.45') FROM root.sg.d; -``` -### 12.2 UDF 注册 - -```sql -CREATE FUNCTION AS (USING URI URI-STRING)? -``` - -#### 不指定URI -```sql -CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' -``` -#### 指定URI -```sql -CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' USING URI 'http://jar/example.jar' -``` -### 12.3 UDF 卸载 - -```sql -DROP FUNCTION -``` -```sql -DROP FUNCTION example -``` -### 12.4 UDF 查询 - -#### 带自定义输入参数的查询 -```sql -SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; -``` -```sql -SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; -``` -#### 与其他查询的嵌套查询 -```sql -SELECT s1, s2, example(s1, s2) FROM root.sg.d1; - -SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; - -SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; - -SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; -``` -### 12.5 查看所有注册的 UDF -```sql -SHOW FUNCTIONS -``` -## 13. 权限管理 - -### 13.1 用户与角色相关 - -- 创建用户(需 MANAGE_USER 权限) - - -```SQL -CREATE USER -eg: CREATE USER user1 'passwd' -``` - -- 删除用户 (需 MANEGE_USER 权限) - - -```SQL -DROP USER -eg: DROP USER user1 -``` - -- 创建角色 (需 MANAGE_ROLE 权限) - -```SQL -CREATE ROLE -eg: CREATE ROLE role1 -``` - -- 删除角色 (需 MANAGE_ROLE 权限) - - -```SQL -DROP ROLE -eg: DROP ROLE role1 -``` - -- 赋予用户角色 (需 MANAGE_ROLE 权限) - - -```SQL -GRANT ROLE TO -eg: GRANT ROLE admin TO user1 -``` - -- 移除用户角色 (需 MANAGE_ROLE 权限) - - -```SQL -REVOKE ROLE FROM -eg: REVOKE ROLE admin FROM user1 -``` - -- 列出所有用户 (需 MANEGE_USER 权限) - -```SQL -LIST USER -``` - -- 列出所有角色 (需 MANAGE_ROLE 权限) - -```SQL -LIST ROLE -``` - -- 列出指定角色下所有用户 (需 MANEGE_USER 权限) - -```SQL -LIST USER OF ROLE -eg: LIST USER OF ROLE roleuser -``` - -- 列出指定用户下所有角色 - -用户可以列出自己的角色,但列出其他用户的角色需要拥有 MANAGE_ROLE 权限。 - -```SQL -LIST ROLE OF USER -eg: LIST ROLE OF USER tempuser -``` - -- 列出用户所有权限 - -用户可以列出自己的权限信息,但列出其他用户的权限需要拥有 MANAGE_USER 权限。 - -```SQL -LIST PRIVILEGES OF USER ; -eg: LIST PRIVILEGES OF USER tempuser; +--- +redirectTo: SQL-Manual_apache.html +--- + \ No newline at end of file diff --git a/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual_apache.md b/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual_apache.md new file mode 100644 index 000000000..72b34374b --- /dev/null +++ b/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual_apache.md @@ -0,0 +1,1973 @@ +# SQL手册 + +## 1. 元数据操作 + +### 1.1 数据库管理 + +#### 创建数据库 + +```sql +CREATE DATABASE root.ln +``` + +#### 查看数据库 + +```sql +show databases +show databases root.* +show databases root.** +``` + +#### 删除数据库 + +```sql +DELETE DATABASE root.ln +DELETE DATABASE root.sgcc +DELETE DATABASE root.** +``` + +#### 统计数据库数量 + +```sql +count databases +count databases root.* +count databases root.sgcc.* +count databases root.sgcc +``` + +### 1.2 时间序列管理 + +#### 创建时间序列 + +```sql +create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- 简化版 + +```sql +create timeseries root.ln.wf01.wt01.status BOOLEAN +create timeseries root.ln.wf01.wt01.temperature FLOAT +create timeseries root.ln.wf02.wt02.hardware TEXT +create timeseries root.ln.wf02.wt02.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature FLOAT +``` + +- 错误提示 + +```sql +create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +> error: encoding TS_2DIFF does not support BOOLEAN +``` + +#### 创建对齐时间序列 + +```sql +CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOAT) +``` + +#### 删除时间序列 + +```sql +delete timeseries root.ln.wf01.wt01.status +delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +delete timeseries root.ln.wf02.* +drop timeseries root.ln.wf02.* +``` + +#### 查看时间序列 + +```sql +SHOW TIMESERIES +SHOW TIMESERIES +SHOW TIMESERIES root.** +SHOW TIMESERIES root.ln.** +SHOW TIMESERIES root.ln.** limit 10 offset 10 +SHOW TIMESERIES root.ln.** where timeseries contains 'wf01.wt' +SHOW TIMESERIES root.ln.** where dataType=FLOAT +SHOW TIMESERIES root.ln.** where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +SHOW LATEST TIMESERIES +``` + +#### 统计时间序列数量 + +```sql +COUNT TIMESERIES root.** +COUNT TIMESERIES root.ln.** +COUNT TIMESERIES root.ln.*.*.status +COUNT TIMESERIES root.ln.wf01.wt01.status +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +COUNT TIMESERIES root.** WHERE time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +COUNT TIMESERIES root.** GROUP BY LEVEL=1 +COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +#### 标签点管理 + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +- 重命名标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +- 重新设置标签或属性的值 + +```sql +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +- 删除已经存在的标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +- 添加新的标签 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +- 添加新的属性 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +- 更新插入别名,标签和属性 + +```sql +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +- 使用标签作为过滤条件查询时间序列 + +```sql +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +返回给定路径的下的所有满足条件的时间序列信息: + +```sql +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- 使用标签作为过滤条件统计时间序列数量 + +```sql +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +返回给定路径的下的所有满足条件的时间序列的数量: + +```sql +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +创建对齐时间序列: + +```sql +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +支持查询: + +```sql +show timeseries where TAGS(tag1)='v1' +``` + +### 1.3 时间序列路径管理 + +#### 查看路径的所有子路径 + +```sql +SHOW CHILD PATHS pathPattern +- 查询 root.ln 的下一层:show child paths root.ln +- 查询形如 root.xx.xx.xx 的路径:show child paths root.*.* +``` +#### 查看路径的所有子节点 + +```sql +SHOW CHILD NODES pathPattern + +- 查询 root 的下一层:show child nodes root +- 查询 root.ln 的下一层 :show child nodes root.ln +``` +#### 查看设备 + +```sql +IoTDB> show devices + +IoTDB> show devices root.ln.** + +IoTDB> show devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +##### 查看设备及其 database 信息 + +```sql +IoTDB> show devices with database + +IoTDB> show devices root.ln.** with database +``` +#### 统计节点数 + +```sql +IoTDB > COUNT NODES root.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.wf01.* LEVEL=3 + +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` +#### 统计设备数量 + +```sql + +IoTDB> count devices + +IoTDB> count devices root.ln.** + +IoTDB> count devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +### 1.4 设备模板管理 + + +![img](/img/%E6%A8%A1%E6%9D%BF.png) +![img](/img/template.jpg) + + + +#### 创建设备模板 + +```Go +CREATE DEVICE TEMPLATE ALIGNED? '(' [',' ]+ ')' +``` + +创建包含两个非对齐序列的设备模板 +```sql +IoTDB> create device template t1 (temperature FLOAT, status BOOLEAN) +``` +创建包含一组对齐序列的设备模板 +```sql +IoTDB> create device template t2 aligned (lat FLOAT, lon FLOAT) +``` +#### 挂载设备模板 +```sql +IoTDB> set DEVICE TEMPLATE t1 to root.sg1 +``` +#### 激活设备模板 +```sql +IoTDB> create timeseries using DEVICE TEMPLATE on root.sg1.d1 + +IoTDB> set DEVICE TEMPLATE t1 to root.sg1.d1 + +IoTDB> set DEVICE TEMPLATE t2 to root.sg1.d2 + +IoTDB> create timeseries using device template on root.sg1.d1 + +IoTDB> create timeseries using device template on root.sg1.d2 +``` +#### 查看设备模板 +```sql +IoTDB> show device templates +``` +- 查看某个设备模板下的物理量 +```sql +IoTDB> show nodes in device template t1 +``` +- 查看挂载了某个设备模板的路径 +```sql +IoTDB> show paths set device template t1 +``` +- 查看使用了某个设备模板的路径(即模板在该路径上已激活,序列已创建) +```sql +IoTDB> show paths using device template t1 +``` +#### 解除设备模板 +```sql +IoTDB> delete timeseries of device template t1 from root.sg1.d1 +``` +```sql +IoTDB> deactivate device template t1 from root.sg1.d1 +``` +批量处理 +```sql +IoTDB> delete timeseries of device template t1 from root.sg1.*, root.sg2.* +``` +```sql +IoTDB> deactivate device template t1 from root.sg1.*, root.sg2.* +``` +#### 卸载设备模板 +```sql +IoTDB> unset device template t1 from root.sg1.d1 +``` +#### 删除设备模板 +```sql +IoTDB> drop device template t1 +``` +### 1.5 数据存活时间管理 + +#### 设置 TTL +```sql +IoTDB> set ttl to root.ln 3600000 +``` +```sql +IoTDB> set ttl to root.sgcc.** 3600000 +``` +```sql +IoTDB> set ttl to root.** 3600000 +``` +#### 取消 TTL +```sql +IoTDB> unset ttl from root.ln +``` +```sql +IoTDB> unset ttl from root.sgcc.** +``` +```sql +IoTDB> unset ttl from root.** +``` + +#### 显示 TTL +```sql +IoTDB> SHOW ALL TTL +``` +```sql +IoTDB> SHOW TTL ON pathPattern +``` +```sql +IoTDB> show DEVICES +``` +## 2. 写入数据 + +### 2.1 写入单列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1'),(2, 'v1') +``` +### 2.2 写入多列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) values (2, false, 'v2') +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` +### 2.3 使用服务器时间戳 +```sql +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` +### 2.4 写入对齐时间序列数据 +```sql +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(1, 1, 1) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +``` +```sql +IoTDB > select * from root.sg1.d1 +``` +### 2.5 加载 TsFile 文件数据 + +load '' [sglevel=int][onSuccess=delete/none] + +#### 通过指定文件路径(绝对路径)加载单 tsfile 文件 + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +#### 通过指定文件夹路径(绝对路径)批量加载文件 + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +## 3. 删除数据 + +### 3.1 删除单列数据 +```sql +delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time < 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time > 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time >= 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time = 20 +``` +出错: +```sql +delete from root.ln.wf02.wt02.status where time > 4 or time < 0 + +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic + +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +``` + +删除时间序列中的所有数据: +```sql +delete from root.ln.wf02.wt02.status +``` +### 3.2 删除多列数据 +```sql +delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +``` +声明式的编程方式: +```sql +IoTDB> delete from root.ln.wf03.wt02.status where time < now() + +Msg: The statement is executed successfully. +``` +## 4. 数据查询 + +### 4.1 基础查询 + +#### 时间过滤查询 +```sql +select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` +#### 根据一个时间区间选择多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 按照多个时间区间选择同一设备的多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 按照多个时间区间选择不同设备的多列数据 +```sql +select wf01.wt01.status, wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 根据时间降序返回结果集 +```sql +select * from root.ln.** where time > 1 order by time desc limit 10; +``` +### 4.2 选择表达式 + +#### 使用别名 +```sql +select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` +#### 运算符 + +#### 函数 + +不支持: +```sql +select s1, count(s1) from root.sg.d1; + +select sin(s1), count(s1) from root.sg.d1; + +select s1, count(s1) from root.sg.d1 group by ([10,100),10ms); +``` +##### 时间序列查询嵌套表达式 + +示例 1: +```sql +select a, + +​ b, + +​ ((a + 1) * 2 - 1) % 2 + 1.5, + +​ sin(a + sin(a + sin(b))), + +​ -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 + +from root.sg1; +``` +示例 2: +```sql +select (a + b) * 2 + sin(a) from root.sg + +示例 3: + +select (a + *) / 2 from root.sg1 + +示例 4: + +select (a + b) * 3 from root.sg, root.ln +``` +##### 聚合查询嵌套表达式 + +示例 1: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) + +from root.ln.wf01.wt01; +``` +示例 2: +```sql +select avg(*), + +​ (avg(*) + 1) * 3 / 2 -1 + +from root.sg1 +``` +示例 3: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) as custom_sum + +from root.ln.wf01.wt01 + +GROUP BY([10, 90), 10ms); +``` +#### 最新点查询 + +SQL 语法: + +```Go +select last [COMMA ]* from < PrefixPath > [COMMA < PrefixPath >]* [ORDER BY TIMESERIES (DESC | ASC)?] +``` + +查询 root.ln.wf01.wt01.status 的最新数据点 +```sql +IoTDB> select last status from root.ln.wf01.wt01 +``` +查询 root.ln.wf01.wt01 下 status,temperature 时间戳大于等于 2017-11-07T23:50:00 的最新数据点 +```sql +IoTDB> select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +``` +查询 root.ln.wf01.wt01 下所有序列的最新数据点,并按照序列名降序排列 +```sql +IoTDB> select last * from root.ln.wf01.wt01 order by timeseries desc; +``` +### 4.3 查询过滤条件 + +#### 时间过滤条件 + +选择时间戳大于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +``` +选择时间戳等于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +``` +选择时间区间 [2017-11-01T00:05:00.000, 2017-11-01T00:12:00.000) 内的数据: +```sql +select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 值过滤条件 + +选择值大于 36.5 的数据: +```sql +select temperature from root.sg1.d1 where temperature > 36.5; +``` +选择值等于 true 的数据: +```sql +select status from root.sg1.d1 where status = true; +``` +选择区间 [36.5,40] 内或之外的数据: +```sql +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +``` +```sql +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +``` +选择值在特定范围内的数据: +```sql +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +``` +选择值在特定范围外的数据: +```sql +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +``` +选择值为空的数据: +```sql +select code from root.sg1.d1 where temperature is null; +``` +选择值为非空的数据: +```sql +select code from root.sg1.d1 where temperature is not null; +``` +#### 模糊查询 + +查询 `root.sg.d1` 下 `value` 含有`'cc'`的数据 +```sql +IoTDB> select * from root.sg.d1 where value like '%cc%' +``` +查询 `root.sg.d1` 下 `value` 中间为 `'b'`、前后为任意单个字符的数据 +```sql +IoTDB> select * from root.sg.device where value like '_b_' +``` +查询 root.sg.d1 下 value 值为26个英文字符组成的字符串 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +``` + +查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 4.4 分段分组聚合 + +#### 未指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` +#### 指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` +滑动步长可以小于聚合窗口 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-01 10:00:00), 4h, 2h); +``` +#### 按照自然月份的时间区间分组聚合查询 +```sql +select count(status) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +每个时间间隔窗口内都有数据 +```sql +select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +#### 左开右闭区间 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` +#### 与分组聚合混合使用 + +统计降采样后的数据点个数 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +``` +加上滑动 Step 的降采样后的结果也可以汇总 +```sql +select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` +#### 路径层级分组聚合 + +统计不同 database 下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1 +``` +统计不同设备下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 3 +``` +统计不同 database 下的不同设备中 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1, 3 +``` +查询所有序列下温度传感器 temperature 的最大值 +```sql +select max_value(temperature) from root.** group by level = 0 +``` +查询某一层级下所有传感器拥有的总数据点数 +```sql +select count(*) from root.ln.** group by level = 2 +``` +#### 标签分组聚合 + +##### 单标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` +##### 多标签聚合查询 +```sql +SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` +##### 基于时间区间的标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` +#### 差值分段聚合 +```sql +group by variation(controlExpression[,delta][,ignoreNull=true/false]) +``` +##### delta=0时的等值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +``` +指定ignoreNull为false +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +``` +##### delta!=0时的差值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +``` +#### 条件分段聚合 +```sql +group by condition(predict,[keep>/>=/=/<=/<]threshold,[,ignoreNull=true/false]) +``` +查询至少连续两行以上的charging_status=1的数据 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=true) +``` +当设置`ignoreNull`为false时,遇到null值为将其视为一个不满足条件的行,得到结果原先的分组被含null的行拆分 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=false) +``` +#### 会话分段聚合 +```sql +group by session(timeInterval) +``` +按照不同的时间单位设定时间间隔 +```sql +select __endTime,count(*) from root.** group by session(1d) +``` +和`HAVING`、`ALIGN BY DEVICE`共同使用 +```sql +select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` +#### 点数分段聚合 +```sql +group by count(controlExpression, size[,ignoreNull=true/false]) +``` +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) + +当使用ignoreNull将null值也考虑进来 +```sql +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` +### 4.5 聚合结果过滤 + +不正确的: +```sql +select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 + +select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 + +select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 + +select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` +SQL 示例: +```sql + select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 2; + + select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device; +``` +### 4.6 结果集补空值 +```sql +FILL '(' PREVIOUS | LINEAR | constant (, interval=DURATION_LITERAL)? ')' +``` +#### `PREVIOUS` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` +#### `PREVIOUS` 填充并指定填充超时阈值 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` +#### `LINEAR` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` +#### 常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +``` +使用 `BOOLEAN` 类型的常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` +### 4.7 查询结果分页 + +#### 按行分页 + +基本的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 10 +``` +带 `OFFSET` 的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +``` +`LIMIT` 子句与 `WHERE` 子句结合 +```sql +select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 +``` +`LIMIT` 子句与 `GROUP BY` 子句组合 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 +``` +#### 按列分页 + +基本的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +``` +带 `SOFFSET` 的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `GROUP BY` 子句结合 +```sql +select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `LIMIT` 子句结合 +```sql +select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` +### 4.8 排序 + +时间对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` +设备对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +``` +在时间戳相等时按照设备名排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +``` +没有显式指定时 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +对聚合后的结果进行排序 +```sql +select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` +### 4.9 查询对齐模式 + +#### 按设备对齐 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +### 4.10 查询写回(SELECT INTO) + +#### 整体描述 +```sql +selectIntoStatement + +​ : SELECT + +​ resultColumn [, resultColumn] ... + +​ INTO intoItem [, intoItem] ... + +​ FROM prefixPath [, prefixPath] ... + +​ [WHERE whereCondition] + +​ [GROUP BY groupByTimeClause, groupByLevelClause] + +​ [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + +​ [LIMIT rowLimit OFFSET rowOffset] + +​ [ALIGN BY DEVICE] + +​ ; + + + +intoItem + +​ : [ALIGNED] intoDevicePath '(' intoMeasurementName [',' intoMeasurementName]* ')' + +​ ; +``` +按时间对齐,将 `root.sg` database 下四条序列的查询结果写入到 `root.sg_copy` database 下指定的四条序列中 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +``` +按时间对齐,将聚合查询的结果存储到指定序列中 +```sql +IoTDB> select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +``` +按设备对齐 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +按设备对齐,将表达式计算的结果存储到指定序列中 +```sql +IoTDB> select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` +#### 使用变量占位符 + +##### 按时间对齐(默认) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2 + +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) + +from root.sg.d1, root.sg.d2; +``` + +该语句等价于: +``` + +select s1, s2 + +into root.sg_copy.d1(s1), root.sg_copy.d2(s1), root.sg_copy.d1(s2), root.sg_copy.d2(s2) + +from root.sg.d1, root.sg.d2; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 + +``` +select d1.s1, d1.s2, d2.s3, d3.s4 + +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) + +from root.sg; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 + +``` +select * into root.sg_bk.::(::) from root.sg.**; +``` + +##### 按设备对齐(使用 `ALIGN BY DEVICE`) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2, s3, s4 + +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) + +from root.sg.d1, root.sg.d2, root.sg.d3 + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 +``` + +select avg(s1), sum(s2) + sum(s3), count(s4) + +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) + +from root.** + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select * into ::(backup_${4}) from root.sg.** align by device; +``` + +#### 指定目标序列为对齐序列 +``` + +select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +## 5. 运维语句 +生成对应的查询计划 +``` +explain select s1,s2 from root.sg.d1 +``` +执行对应的查询语句,并获取分析结果 +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 6. 运算符 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md) + +### 6.1 算数运算符 + +更多见文档 [Arithmetic Operators and Functions](./Operator-and-Expression.md#算数运算符) + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 6.2 比较运算符 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较运算符) + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 6.3 逻辑运算符 + +更多见文档[Logical Operators](./Operator-and-Expression.md#逻辑运算符) + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 7. 内置函数 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md#聚合函数) + +### 7.1 Aggregate Functions + +更多见文档[Aggregate Functions](./Operator-and-Expression.md#聚合函数) + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 7.2 算数函数 + +更多见文档[Arithmetic Operators and Functions](./Operator-and-Expression.md#数学函数) + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 7.3 比较函数 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较函数) + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 7.4 字符串处理函数 + +更多见文档[String Processing](./Operator-and-Expression.md#字符串函数) + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 7.5 数据类型转换函数 + +更多见文档[Data Type Conversion Function](./Operator-and-Expression.md#数据类型转换函数) + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 7.6 常序列生成函数 + +更多见文档[Constant Timeseries Generating Functions](./Operator-and-Expression.md#常序列生成函数) + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 7.7 选择函数 + +更多见文档[Selector Functions](./Operator-and-Expression.md#选择函数) + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 7.8 区间查询函数 + +更多见文档[Continuous Interval Functions](./Operator-and-Expression.md#区间查询函数) + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 7.9 趋势计算函数 + +更多见文档[Variation Trend Calculation Functions](./Operator-and-Expression.md#趋势计算函数) + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 7.10 采样函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#采样函数)。 +### 7.11 时间序列处理函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#时间序列处理函数)。 + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 7.12 时间序列处理函数 + +更多见文档[Time-Series](./Operator-and-Expression.md#时间序列处理函数) + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 8. 数据质量函数库 + +更多见文档[UDF-Libraries](../SQL-Manual/UDF-Libraries.md) + +### 8.1 数据质量 + +更多见文档[Data-Quality](../SQL-Manual/UDF-Libraries.md#数据质量) + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 8.2 数据画像 + +更多见文档[Data-Profiling](../SQL-Manual/UDF-Libraries.md#数据画像) + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 8.3 异常检测 + +更多见文档[Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#异常检测) + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 8.4 频域分析 + +更多见文档[Frequency-Domain](../SQL-Manual/UDF-Libraries.md#频域分析) + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 8.5 数据匹配 + +更多见文档[Data-Matching](../SQL-Manual/UDF-Libraries.md#数据匹配) + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 8.6 数据修复 + +更多见文档[Data-Repairing](../SQL-Manual/UDF-Libraries.md#数据修复) + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 8.7 序列发现 + +更多见文档[Series-Discovery](../SQL-Manual/UDF-Libraries.md#序列发现) + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 8.8 机器学习 + +更多见文档[Machine-Learning](../SQL-Manual/UDF-Libraries.md#机器学习) + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 9. Lambda 表达式 + +更多见文档[Lambda](./Operator-and-Expression.md#lambda-表达式) + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 10. 条件表达式 + +更多见文档[Conditional Expressions](./Operator-and-Expression.md#条件表达式) + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +#### 配置连续查询执行的周期性间隔 +```sql +CREATE CONTINUOUS QUERY cq1 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询的时间窗口大小 +``` +CREATE CONTINUOUS QUERY cq2 + +RESAMPLE RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 同时配置连续查询执行的周期性间隔和时间窗口大小 +```sql +CREATE CONTINUOUS QUERY cq3 + +RESAMPLE EVERY 20s RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询每次查询执行时间窗口的结束时间 +```sql +CREATE CONTINUOUS QUERY cq4 + +RESAMPLE EVERY 20s RANGE 40s, 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 没有GROUP BY TIME子句的连续查询 +```sql +CREATE CONTINUOUS QUERY cq5 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT temperature + 1 + + INTO root.precalculated_sg.::(temperature) + + FROM root.ln.*.* + + align by device + +END + + + +\> SELECT temperature from root.precalculated_sg.*.* align by device; +``` +### 11.2 连续查询的管理 + +#### 查询系统已有的连续查询 + +展示集群中所有的已注册的连续查询 +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` +```sql +SHOW CONTINUOUS QUERIES; +``` +#### 删除已有的连续查询 + +删除指定的名为cq_id的连续查询: + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` +```sql +DROP CONTINUOUS QUERY s1_count_cq; +``` +#### 作为子查询的替代品 + +1. 创建一个连续查询 +```sql +CREATE CQ s1_count_cq + +BEGIN + +​ SELECT count(s1) + +​ INTO root.sg_count.d.count_s1 + +​ FROM root.sg.d + +​ GROUP BY(30m) + +END +``` +1. 查询连续查询的结果 +```sql +SELECT avg(count_s1) from root.sg_count.d; +``` +## 12. 用户自定义函数 + +### 12.1 UDFParameters +```sql +SELECT UDF(s1, s2, 'key1'='iotdb', 'key2'='123.45') FROM root.sg.d; +``` +### 12.2 UDF 注册 + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +#### 不指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' +``` +#### 指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' USING URI 'http://jar/example.jar' +``` +### 12.3 UDF 卸载 + +```sql +DROP FUNCTION +``` +```sql +DROP FUNCTION example +``` +### 12.4 UDF 查询 + +#### 带自定义输入参数的查询 +```sql +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +``` +```sql +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; +``` +#### 与其他查询的嵌套查询 +```sql +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; + +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; + +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; + +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` +### 12.5 查看所有注册的 UDF +```sql +SHOW FUNCTIONS +``` +## 13. 权限管理 + +### 13.1 用户与角色相关 + +- 创建用户(需 MANAGE_USER 权限) + + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- 删除用户 (需 MANEGE_USER 权限) + + +```SQL +DROP USER +eg: DROP USER user1 +``` + +- 创建角色 (需 MANAGE_ROLE 权限) + +```SQL +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- 删除角色 (需 MANAGE_ROLE 权限) + + +```SQL +DROP ROLE +eg: DROP ROLE role1 +``` + +- 赋予用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- 移除用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- 列出所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER +``` + +- 列出所有角色 (需 MANAGE_ROLE 权限) + +```SQL +LIST ROLE +``` + +- 列出指定角色下所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- 列出指定用户下所有角色 + +用户可以列出自己的角色,但列出其他用户的角色需要拥有 MANAGE_ROLE 权限。 + +```SQL +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- 列出用户所有权限 + +用户可以列出自己的权限信息,但列出其他用户的权限需要拥有 MANAGE_USER 权限。 + +```SQL +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; + +``` + +- 列出角色所有权限 + +用户可以列出自己具有的角色的权限信息,列出其他角色的权限需要有 MANAGE_ROLE 权限。 + +```SQL +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- 更新密码 + +用户可以更新自己的密码,但更新其他用户密码需要具备MANAGE_USER 权限。 + +```SQL +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 13.2 授权与取消授权 + +用户使用授权语句对赋予其他用户权限,语法如下: + +```SQL +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +用户使用取消授权语句可以将其他的权限取消,语法如下: + +```SQL +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` + diff --git a/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual_timecho.md b/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual_timecho.md new file mode 100644 index 000000000..403546035 --- /dev/null +++ b/src/zh/UserGuide/latest/SQL-Manual/SQL-Manual_timecho.md @@ -0,0 +1,1898 @@ +# SQL手册 + +## 1. 元数据操作 + +### 1.1 数据库管理 + +#### 创建数据库 + +```sql +CREATE DATABASE root.ln +``` + +#### 查看数据库 + +```sql +show databases +show databases root.* +show databases root.** +``` + +#### 删除数据库 + +```sql +DELETE DATABASE root.ln +DELETE DATABASE root.sgcc +DELETE DATABASE root.** +``` + +#### 统计数据库数量 + +```sql +count databases +count databases root.* +count databases root.sgcc.* +count databases root.sgcc +``` + +### 1.2 时间序列管理 + +#### 创建时间序列 + +```sql +create timeseries root.ln.wf01.wt01.status with datatype=BOOLEAN +create timeseries root.ln.wf01.wt01.temperature with datatype=FLOAT +create timeseries root.ln.wf02.wt02.hardware with datatype=TEXT +create timeseries root.ln.wf02.wt02.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.status with datatype=BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature with datatype=FLOAT +``` + +- 简化版 + +```sql +create timeseries root.ln.wf01.wt01.status BOOLEAN +create timeseries root.ln.wf01.wt01.temperature FLOAT +create timeseries root.ln.wf02.wt02.hardware TEXT +create timeseries root.ln.wf02.wt02.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.status BOOLEAN +create timeseries root.sgcc.wf03.wt01.temperature FLOAT +``` + +- 错误提示 + +```sql +create timeseries root.ln.wf02.wt02.status WITH DATATYPE=BOOLEAN +> error: encoding TS_2DIFF does not support BOOLEAN +``` + +#### 创建对齐时间序列 + +```sql +CREATE ALIGNED TIMESERIES root.ln.wf01.GPS(latitude FLOAT, longitude FLOAT) +``` + +#### 删除时间序列 + +```sql +delete timeseries root.ln.wf01.wt01.status +delete timeseries root.ln.wf01.wt01.temperature, root.ln.wf02.wt02.hardware +delete timeseries root.ln.wf02.* +drop timeseries root.ln.wf02.* +``` + +#### 查看时间序列 + +```sql +SHOW TIMESERIES +SHOW TIMESERIES +SHOW TIMESERIES root.** +SHOW TIMESERIES root.ln.** +SHOW TIMESERIES root.ln.** limit 10 offset 10 +SHOW TIMESERIES root.ln.** where timeseries contains 'wf01.wt' +SHOW TIMESERIES root.ln.** where dataType=FLOAT +SHOW TIMESERIES root.ln.** where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +SHOW LATEST TIMESERIES +``` + +#### 统计时间序列数量 + +```sql +COUNT TIMESERIES root.** +COUNT TIMESERIES root.ln.** +COUNT TIMESERIES root.ln.*.*.status +COUNT TIMESERIES root.ln.wf01.wt01.status +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' +COUNT TIMESERIES root.** WHERE DATATYPE = INT64 +COUNT TIMESERIES root.** WHERE TAGS(unit) contains 'c' +COUNT TIMESERIES root.** WHERE TAGS(unit) = 'c' +COUNT TIMESERIES root.** WHERE TIMESERIES contains 'sgcc' group by level = 1 +COUNT TIMESERIES root.** WHERE time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +COUNT TIMESERIES root.** GROUP BY LEVEL=1 +COUNT TIMESERIES root.ln.** GROUP BY LEVEL=2 +COUNT TIMESERIES root.ln.wf01.* GROUP BY LEVEL=2 +``` + +#### 标签点管理 + +```sql +create timeseries root.turbine.d1.s1(temprature) with datatype=FLOAT tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2) +``` + +- 重命名标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 RENAME tag1 TO newTag1 +``` + +- 重新设置标签或属性的值 + +```sql +ALTER timeseries root.turbine.d1.s1 SET newTag1=newV1, attr1=newV1 +``` + +- 删除已经存在的标签或属性 + +```sql +ALTER timeseries root.turbine.d1.s1 DROP tag1, tag2 +``` + +- 添加新的标签 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD TAGS tag3=v3, tag4=v4 +``` + +- 添加新的属性 + +```sql +ALTER timeseries root.turbine.d1.s1 ADD ATTRIBUTES attr3=v3, attr4=v4 +``` + +- 更新插入别名,标签和属性 + +```sql +ALTER timeseries root.turbine.d1.s1 UPSERT ALIAS=newAlias TAGS(tag2=newV2, tag3=v3) ATTRIBUTES(attr3=v3, attr4=v4) +``` + +- 使用标签作为过滤条件查询时间序列 + +```sql +SHOW TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +``` + +返回给定路径的下的所有满足条件的时间序列信息: + +```sql +ALTER timeseries root.ln.wf02.wt02.hardware ADD TAGS unit=c +ALTER timeseries root.ln.wf02.wt02.status ADD TAGS description=test1 +show timeseries root.ln.** where TAGS(unit)='c' +show timeseries root.ln.** where TAGS(description) contains 'test1' +``` + +- 使用标签作为过滤条件统计时间序列数量 + +```sql +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause +COUNT TIMESERIES (<`PathPattern`>)? timeseriesWhereClause GROUP BY LEVEL= +``` + +返回给定路径的下的所有满足条件的时间序列的数量: + +```sql +count timeseries +count timeseries root.** where TAGS(unit)='c' +count timeseries root.** where TAGS(unit)='c' group by level = 2 +``` + +创建对齐时间序列: + +```sql +create aligned timeseries root.sg1.d1(s1 INT32 tags(tag1=v1, tag2=v2) attributes(attr1=v1, attr2=v2), s2 DOUBLE tags(tag3=v3, tag4=v4) attributes(attr3=v3, attr4=v4)) +``` + +支持查询: + +```sql +show timeseries where TAGS(tag1)='v1' +``` + +### 1.3 时间序列路径管理 + +#### 查看路径的所有子路径 + +```sql +SHOW CHILD PATHS pathPattern +- 查询 root.ln 的下一层:show child paths root.ln +- 查询形如 root.xx.xx.xx 的路径:show child paths root.*.* +``` +#### 查看路径的所有子节点 + +```sql +SHOW CHILD NODES pathPattern + +- 查询 root 的下一层:show child nodes root +- 查询 root.ln 的下一层 :show child nodes root.ln +``` +#### 查看设备 + +```sql +IoTDB> show devices + +IoTDB> show devices root.ln.** + +IoTDB> show devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +##### 查看设备及其 database 信息 + +```sql +IoTDB> show devices with database + +IoTDB> show devices root.ln.** with database +``` +#### 统计节点数 + +```sql +IoTDB > COUNT NODES root.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.** LEVEL=2 + +IoTDB > COUNT NODES root.ln.wf01.* LEVEL=3 + +IoTDB > COUNT NODES root.**.temperature LEVEL=3 +``` +#### 统计设备数量 + +```sql + +IoTDB> count devices + +IoTDB> count devices root.ln.** + +IoTDB> count devices where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` + +### 1.4 数据存活时间管理 + +#### 设置 TTL +```sql +IoTDB> set ttl to root.ln 3600000 +``` +```sql +IoTDB> set ttl to root.sgcc.** 3600000 +``` +```sql +IoTDB> set ttl to root.** 3600000 +``` +#### 取消 TTL +```sql +IoTDB> unset ttl from root.ln +``` +```sql +IoTDB> unset ttl from root.sgcc.** +``` +```sql +IoTDB> unset ttl from root.** +``` + +#### 显示 TTL +```sql +IoTDB> SHOW ALL TTL +``` +```sql +IoTDB> SHOW TTL ON pathPattern +``` +```sql +IoTDB> show DEVICES +``` +## 2. 写入数据 + +### 2.1 写入单列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,status) values(1,true) +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp,hardware) values(1, 'v1'),(2, 'v1') +``` +### 2.2 写入多列数据 +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) values (2, false, 'v2') +``` +```sql +IoTDB > insert into root.ln.wf02.wt02(timestamp, status, hardware) VALUES (3, false, 'v3'),(4, true, 'v4') +``` +### 2.3 使用服务器时间戳 +```sql +IoTDB > insert into root.ln.wf02.wt02(status, hardware) values (false, 'v2') +``` +### 2.4 写入对齐时间序列数据 +```sql +IoTDB > create aligned timeseries root.sg1.d1(s1 INT32, s2 DOUBLE) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(1, 1, 1) +``` +```sql +IoTDB > insert into root.sg1.d1(timestamp, s1, s2) aligned values(2, 2, 2), (3, 3, 3) +``` +```sql +IoTDB > select * from root.sg1.d1 +``` +### 2.5 加载 TsFile 文件数据 + +load '' [sglevel=int][onSuccess=delete/none] + +#### 通过指定文件路径(绝对路径)加载单 tsfile 文件 + +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile'` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' onSuccess=delete` +- `load '/Users/Desktop/data/1575028885956-101-0.tsfile' sglevel=1 onSuccess=delete` + + +#### 通过指定文件夹路径(绝对路径)批量加载文件 + +- `load '/Users/Desktop/data'` +- `load '/Users/Desktop/data' sglevel=1` +- `load '/Users/Desktop/data' onSuccess=delete` +- `load '/Users/Desktop/data' sglevel=1 onSuccess=delete` + +## 3. 删除数据 + +### 3.1 删除单列数据 +```sql +delete from root.ln.wf02.wt02.status where time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time>=2017-01-01T00:00:00 and time<=2017-11-01T16:26:00; +``` +```sql +delete from root.ln.wf02.wt02.status where time < 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time < 20 and time > 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time <= 20 and time >= 10 +``` +```sql +delete from root.ln.wf02.wt02.status where time > 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time >= 20 +``` +```sql +delete from root.ln.wf02.wt02.status where time = 20 +``` +出错: +```sql +delete from root.ln.wf02.wt02.status where time > 4 or time < 0 + +Msg: 303: Check metadata error: For delete statement, where clause can only contain atomic + +expressions like : time > XXX, time <= XXX, or two atomic expressions connected by 'AND' +``` + +删除时间序列中的所有数据: +```sql +delete from root.ln.wf02.wt02.status +``` +### 3.2 删除多列数据 +```sql +delete from root.ln.wf02.wt02.* where time <= 2017-11-01T16:26:00; +``` +声明式的编程方式: +```sql +IoTDB> delete from root.ln.wf03.wt02.status where time < now() + +Msg: The statement is executed successfully. +``` +## 4. 数据查询 + +### 4.1 基础查询 + +#### 时间过滤查询 +```sql +select temperature from root.ln.wf01.wt01 where time < 2017-11-01T00:08:00.000 +``` +#### 根据一个时间区间选择多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 按照多个时间区间选择同一设备的多列数据 +```sql +select status, temperature from root.ln.wf01.wt01 where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 按照多个时间区间选择不同设备的多列数据 +```sql +select wf01.wt01.status, wf02.wt02.hardware from root.ln where (time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000) or (time >= 2017-11-01T16:35:00.000 and time <= 2017-11-01T16:37:00.000); +``` +#### 根据时间降序返回结果集 +```sql +select * from root.ln.** where time > 1 order by time desc limit 10; +``` +### 4.2 选择表达式 + +#### 使用别名 +```sql +select s1 as temperature, s2 as speed from root.ln.wf01.wt01; +``` +#### 运算符 + +#### 函数 + +不支持: +```sql +select s1, count(s1) from root.sg.d1; + +select sin(s1), count(s1) from root.sg.d1; + +select s1, count(s1) from root.sg.d1 group by ([10,100),10ms); +``` +##### 时间序列查询嵌套表达式 + +示例 1: +```sql +select a, + +​ b, + +​ ((a + 1) * 2 - 1) % 2 + 1.5, + +​ sin(a + sin(a + sin(b))), + +​ -(a + b) * (sin(a + b) * sin(a + b) + cos(a + b) * cos(a + b)) + 1 + +from root.sg1; +``` +示例 2: +```sql +select (a + b) * 2 + sin(a) from root.sg + +示例 3: + +select (a + *) / 2 from root.sg1 + +示例 4: + +select (a + b) * 3 from root.sg, root.ln +``` +##### 聚合查询嵌套表达式 + +示例 1: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) + +from root.ln.wf01.wt01; +``` +示例 2: +```sql +select avg(*), + +​ (avg(*) + 1) * 3 / 2 -1 + +from root.sg1 +``` +示例 3: +```sql +select avg(temperature), + +​ sin(avg(temperature)), + +​ avg(temperature) + 1, + +​ -sum(hardware), + +​ avg(temperature) + sum(hardware) as custom_sum + +from root.ln.wf01.wt01 + +GROUP BY([10, 90), 10ms); +``` +#### 最新点查询 + +SQL 语法: + +```Go +select last [COMMA ]* from < PrefixPath > [COMMA < PrefixPath >]* [ORDER BY TIMESERIES (DESC | ASC)?] +``` + +查询 root.ln.wf01.wt01.status 的最新数据点 +```sql +IoTDB> select last status from root.ln.wf01.wt01 +``` +查询 root.ln.wf01.wt01 下 status,temperature 时间戳大于等于 2017-11-07T23:50:00 的最新数据点 +```sql +IoTDB> select last status, temperature from root.ln.wf01.wt01 where time >= 2017-11-07T23:50:00 +``` + 查询 root.ln.wf01.wt01 下所有序列的最新数据点,并按照序列名降序排列 +```sql +IoTDB> select last * from root.ln.wf01.wt01 order by timeseries desc; +``` +### 4.3 查询过滤条件 + +#### 时间过滤条件 + +选择时间戳大于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time > 2022-01-01T00:05:00.000; +``` +选择时间戳等于 2022-01-01T00:05:00.000 的数据: +```sql +select s1 from root.sg1.d1 where time = 2022-01-01T00:05:00.000; +``` +选择时间区间 [2017-11-01T00:05:00.000, 2017-11-01T00:12:00.000) 内的数据: +```sql +select s1 from root.sg1.d1 where time >= 2022-01-01T00:05:00.000 and time < 2017-11-01T00:12:00.000; +``` +#### 值过滤条件 + +选择值大于 36.5 的数据: +```sql +select temperature from root.sg1.d1 where temperature > 36.5; +``` +选择值等于 true 的数据: +```sql +select status from root.sg1.d1 where status = true; +``` +选择区间 [36.5,40] 内或之外的数据: +```sql +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +``` +```sql +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; +``` +选择值在特定范围内的数据: +```sql +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +``` +选择值在特定范围外的数据: +```sql +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +``` +选择值为空的数据: +```sql +select code from root.sg1.d1 where temperature is null; +``` +选择值为非空的数据: +```sql +select code from root.sg1.d1 where temperature is not null; +``` +#### 模糊查询 + +查询 `root.sg.d1` 下 `value` 含有`'cc'`的数据 +```sql +IoTDB> select * from root.sg.d1 where value like '%cc%' +``` +查询 `root.sg.d1` 下 `value` 中间为 `'b'`、前后为任意单个字符的数据 +```sql +IoTDB> select * from root.sg.device where value like '_b_' +``` +查询 root.sg.d1 下 value 值为26个英文字符组成的字符串 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +``` + +查询 root.sg.d1 下 value 值为26个小写英文字符组成的字符串且时间大于100的 +```sql +IoTDB> select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +``` + +### 4.4 分段分组聚合 + +#### 未指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d); +``` +#### 指定滑动步长的时间区间分组聚合查询 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d); +``` +滑动步长可以小于聚合窗口 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-01 10:00:00), 4h, 2h); +``` +#### 按照自然月份的时间区间分组聚合查询 +```sql +select count(status) from root.ln.wf01.wt01 where time > 2017-11-01T01:00:00 group by([2017-11-01T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +每个时间间隔窗口内都有数据 +```sql +select count(status) from root.ln.wf01.wt01 group by([2017-10-31T00:00:00, 2019-11-07T23:00:00), 1mo, 2mo); +``` +#### 左开右闭区间 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d); +``` +#### 与分组聚合混合使用 + +统计降采样后的数据点个数 +```sql +select count(status) from root.ln.wf01.wt01 group by ((2017-11-01T00:00:00, 2017-11-07T23:00:00],1d), level=1; +``` +加上滑动 Step 的降采样后的结果也可以汇总 +```sql +select count(status) from root.ln.wf01.wt01 group by ([2017-11-01 00:00:00, 2017-11-07 23:00:00), 3h, 1d), level=1; +``` +#### 路径层级分组聚合 + +统计不同 database 下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1 +``` + 统计不同设备下 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 3 +``` +统计不同 database 下的不同设备中 status 序列的数据点个数 +```sql +select count(status) from root.** group by level = 1, 3 +``` +查询所有序列下温度传感器 temperature 的最大值 +```sql +select max_value(temperature) from root.** group by level = 0 +``` +查询某一层级下所有传感器拥有的总数据点数 +```sql +select count(*) from root.ln.** group by level = 2 +``` +#### 标签分组聚合 + +##### 单标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY TAGS(city); +``` +##### 多标签聚合查询 +```sql +SELECT avg(temperature) FROM root.factory1.** GROUP BY TAGS(city, workshop); +``` +##### 基于时间区间的标签聚合查询 +```sql +SELECT AVG(temperature) FROM root.factory1.** GROUP BY ([1000, 10000), 5s), TAGS(city, workshop); +``` +#### 差值分段聚合 +```sql +group by variation(controlExpression[,delta][,ignoreNull=true/false]) +``` +##### delta=0时的等值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6) +``` +指定ignoreNull为false +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, ignoreNull=false) +``` +##### delta!=0时的差值事件分段 +```sql +select __endTime, avg(s1), count(s2), sum(s3) from root.sg.d group by variation(s6, 4) +``` +#### 条件分段聚合 +```sql +group by condition(predict,[keep>/>=/=/<=/<]threshold,[,ignoreNull=true/false]) +``` +查询至少连续两行以上的charging_status=1的数据 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=true) +``` +当设置`ignoreNull`为false时,遇到null值为将其视为一个不满足条件的行,得到结果原先的分组被含null的行拆分 +```sql +select max_time(charging_status),count(vehicle_status),last_value(soc) from root.** group by condition(charging_status=1,KEEP>=2,ignoreNull=false) +``` +#### 会话分段聚合 +```sql +group by session(timeInterval) +``` +按照不同的时间单位设定时间间隔 +```sql +select __endTime,count(*) from root.** group by session(1d) +``` +和`HAVING`、`ALIGN BY DEVICE`共同使用 +```sql +select __endTime,sum(hardware) from root.ln.wf02.wt01 group by session(50s) having sum(hardware)>0 align by device +``` +#### 点数分段聚合 +```sql +group by count(controlExpression, size[,ignoreNull=true/false]) +``` +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5) + +当使用ignoreNull将null值也考虑进来 +```sql +select count(charging_stauts), first_value(soc) from root.sg group by count(charging_status,5,ignoreNull=false) +``` +### 4.5 聚合结果过滤 + +不正确的: +```sql +select count(s1) from root.** group by ([1,3),1ms) having sum(s1) > s1 + +select count(s1) from root.** group by ([1,3),1ms) having s1 > 1 + +select count(s1) from root.** group by ([1,3),1ms), level=1 having sum(d1.s1) > 1 + +select count(d1.s1) from root.** group by ([1,3),1ms), level=1 having sum(s1) > 1 +``` +SQL 示例: +```sql + select count(s1) from root.** group by ([1,11),2ms), level=1 having count(s2) > 2; + + select count(s1), count(s2) from root.** group by ([1,11),2ms) having count(s2) > 1 align by device; +``` +### 4.6 结果集补空值 +```sql +FILL '(' PREVIOUS | LINEAR | constant (, interval=DURATION_LITERAL)? ')' +``` +#### `PREVIOUS` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous); +``` +#### `PREVIOUS` 填充并指定填充超时阈值 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(previous, 2m); +``` +#### `LINEAR` 填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(linear); +``` +#### 常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(2.0); +``` +使用 `BOOLEAN` 类型的常量填充 +```sql +select temperature, status from root.sgcc.wf03.wt01 where time >= 2017-11-01T16:37:00.000 and time <= 2017-11-01T16:40:00.000 fill(true); +``` +### 4.7 查询结果分页 + +#### 按行分页 + + 基本的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 10 +``` +带 `OFFSET` 的 `LIMIT` 子句 +```sql +select status, temperature from root.ln.wf01.wt01 limit 5 offset 3 +``` +`LIMIT` 子句与 `WHERE` 子句结合 +```sql +select status,temperature from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time< 2017-11-01T00:12:00.000 limit 5 offset 3 +``` + `LIMIT` 子句与 `GROUP BY` 子句组合 +```sql +select count(status), max_value(temperature) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) limit 4 offset 3 +``` +#### 按列分页 + + 基本的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 +``` +带 `SOFFSET` 的 `SLIMIT` 子句 +```sql +select * from root.ln.wf01.wt01 where time > 2017-11-01T00:05:00.000 and time < 2017-11-01T00:12:00.000 slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `GROUP BY` 子句结合 +```sql +select max_value(*) from root.ln.wf01.wt01 group by ([2017-11-01T00:00:00, 2017-11-07T23:00:00),1d) slimit 1 soffset 1 +``` +`SLIMIT` 子句与 `LIMIT` 子句结合 +```sql +select * from root.ln.wf01.wt01 limit 10 offset 100 slimit 2 soffset 0 +``` +### 4.8 排序 + +时间对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time desc; +``` +设备对齐模式下的排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by device desc,time asc align by device; +``` +在时间戳相等时按照设备名排序 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 order by time asc,device desc align by device; +``` +没有显式指定时 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +对聚合后的结果进行排序 +```sql +select count(*) from root.ln.** group by ((2017-11-01T00:00:00.000+08:00,2017-11-01T00:03:00.000+08:00],1m) order by device asc,time asc align by device +``` +### 4.9 查询对齐模式 + +#### 按设备对齐 +```sql +select * from root.ln.** where time <= 2017-11-01T00:01:00 align by device; +``` +### 4.10 查询写回(SELECT INTO) + +#### 整体描述 +```sql +selectIntoStatement + +​ : SELECT + +​ resultColumn [, resultColumn] ... + +​ INTO intoItem [, intoItem] ... + +​ FROM prefixPath [, prefixPath] ... + +​ [WHERE whereCondition] + +​ [GROUP BY groupByTimeClause, groupByLevelClause] + +​ [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + +​ [LIMIT rowLimit OFFSET rowOffset] + +​ [ALIGN BY DEVICE] + +​ ; + + + +intoItem + +​ : [ALIGNED] intoDevicePath '(' intoMeasurementName [',' intoMeasurementName]* ')' + +​ ; +``` +按时间对齐,将 `root.sg` database 下四条序列的查询结果写入到 `root.sg_copy` database 下指定的四条序列中 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1), root.sg_copy.d2(t1, t2), root.sg_copy.d1(t2) from root.sg.d1, root.sg.d2; +``` +按时间对齐,将聚合查询的结果存储到指定序列中 +```sql +IoTDB> select count(s1 + s2), last_value(s2) into root.agg.count(s1_add_s2), root.agg.last_value(s2) from root.sg.d1 group by ([0, 100), 10ms); +``` +按设备对齐 +```sql +IoTDB> select s1, s2 into root.sg_copy.d1(t1, t2), root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +按设备对齐,将表达式计算的结果存储到指定序列中 +```sql +IoTDB> select s1 + s2 into root.expr.add(d1s1_d1s2), root.expr.add(d2s1_d2s2) from root.sg.d1, root.sg.d2 align by device; +``` +#### 使用变量占位符 + +##### 按时间对齐(默认) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2 + +into root.sg_copy.d1(::), root.sg_copy.d2(s1), root.sg_copy.d1(${3}), root.sg_copy.d2(::) + +from root.sg.d1, root.sg.d2; +``` + +该语句等价于: +``` + +select s1, s2 + +into root.sg_copy.d1(s1), root.sg_copy.d2(s1), root.sg_copy.d1(s2), root.sg_copy.d2(s2) + +from root.sg.d1, root.sg.d2; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 + +``` +select d1.s1, d1.s2, d2.s3, d3.s4 + +into ::(s1_1, s2_2), root.sg.d2_2(s3_3), root.${2}_copy.::(s4) + +from root.sg; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 + +``` +select * into root.sg_bk.::(::) from root.sg.**; +``` + +##### 按设备对齐(使用 `ALIGN BY DEVICE`) + +###### 目标设备不使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select s1, s2, s3, s4 + +into root.backup_sg.d1(s1, s2, s3, s4), root.backup_sg.d2(::), root.sg.d3(backup_${4}) + +from root.sg.d1, root.sg.d2, root.sg.d3 + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表不使用变量占位符 +``` + +select avg(s1), sum(s2) + sum(s3), count(s4) + +into root.agg_${2}.::(avg_s1, sum_s2_add_s3, count_s4) + +from root.** + +align by device; +``` + +###### 目标设备使用变量占位符 & 目标物理量列表使用变量占位符 +``` + +select * into ::(backup_${4}) from root.sg.** align by device; +``` + +#### 指定目标序列为对齐序列 +``` + +select s1, s2 into root.sg_copy.d1(t1, t2), aligned root.sg_copy.d2(t1, t2) from root.sg.d1, root.sg.d2 align by device; +``` +## 5. 运维语句 +生成对应的查询计划 +``` +explain select s1,s2 from root.sg.d1 +``` +执行对应的查询语句,并获取分析结果 +``` +explain analyze select s1,s2 from root.sg.d1 order by s1 +``` +## 6. 运算符 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md) + +### 6.1 算数运算符 + +更多见文档 [Arithmetic Operators and Functions](./Operator-and-Expression.md#算数运算符) + +```sql +select s1, - s1, s2, + s2, s1 + s2, s1 - s2, s1 * s2, s1 / s2, s1 % s2 from root.sg.d1 +``` + +### 6.2 比较运算符 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较运算符) + +```sql +# Basic comparison operators +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; + +# `BETWEEN ... AND ...` operator +select temperature from root.sg1.d1 where temperature between 36.5 and 40; +select temperature from root.sg1.d1 where temperature not between 36.5 and 40; + +# Fuzzy matching operator: Use `Like` for fuzzy matching +select * from root.sg.d1 where value like '%cc%' +select * from root.sg.device where value like '_b_' + +# Fuzzy matching operator: Use `Regexp` for fuzzy matching +select * from root.sg.d1 where value regexp '^[A-Za-z]+$' +select * from root.sg.d1 where value regexp '^[a-z]+$' and time > 100 +select b, b like '1%', b regexp '[0-2]' from root.test; + +# `IS NULL` operator +select code from root.sg1.d1 where temperature is null; +select code from root.sg1.d1 where temperature is not null; + +# `IN` operator +select code from root.sg1.d1 where code in ('200', '300', '400', '500'); +select code from root.sg1.d1 where code not in ('200', '300', '400', '500'); +select a, a in (1, 2) from root.test; +``` + +### 6.3 逻辑运算符 + +更多见文档[Logical Operators](./Operator-and-Expression.md#逻辑运算符) + +```sql +select a, b, a > 10, a <= b, !(a <= b), a > 10 && a > b from root.test; +``` + +## 7. 内置函数 + +更多见文档[Operator-and-Expression](./Operator-and-Expression.md#聚合函数) + +### 7.1 Aggregate Functions + +更多见文档[Aggregate Functions](./Operator-and-Expression.md#聚合函数) + +```sql +select count(status) from root.ln.wf01.wt01; + +select count_if(s1=0 & s2=0, 3), count_if(s1=1 & s2=0, 3) from root.db.d1; +select count_if(s1=0 & s2=0, 3, 'ignoreNull'='false'), count_if(s1=1 & s2=0, 3, 'ignoreNull'='false') from root.db.d1; + +select time_duration(s1) from root.db.d1; +``` + +### 7.2 算数函数 + +更多见文档[Arithmetic Operators and Functions](./Operator-and-Expression.md#数学函数) + +```sql +select s1, sin(s1), cos(s1), tan(s1) from root.sg1.d1 limit 5 offset 1000; +select s4,round(s4),round(s4,2),round(s4,-1) from root.sg1.d1; +``` + +### 7.3 比较函数 + +更多见文档[Comparison Operators and Functions](./Operator-and-Expression.md#比较函数) + +```sql +select ts, on_off(ts, 'threshold'='2') from root.test; +select ts, in_range(ts, 'lower'='2', 'upper'='3.1') from root.test; +``` + +### 7.4 字符串处理函数 + +更多见文档[String Processing](./Operator-and-Expression.md#字符串函数) + +```sql +select s1, string_contains(s1, 's'='warn') from root.sg1.d4; +select s1, string_matches(s1, 'regex'='[^\\s]+37229') from root.sg1.d4; +select s1, length(s1) from root.sg1.d1 +select s1, locate(s1, "target"="1") from root.sg1.d1 +select s1, locate(s1, "target"="1", "reverse"="true") from root.sg1.d1 +select s1, startswith(s1, "target"="1") from root.sg1.d1 +select s1, endswith(s1, "target"="1") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB") from root.sg1.d1 +select s1, s2, concat(s1, s2, "target1"="IoT", "target2"="DB", "series_behind"="true") from root.sg1.d1 +select s1, substring(s1 from 1 for 2) from root.sg1.d1 +select s1, replace(s1, 'es', 'tt') from root.sg1.d1 +select s1, upper(s1) from root.sg1.d1 +select s1, lower(s1) from root.sg1.d1 +select s3, trim(s3) from root.sg1.d1 +select s1, s2, strcmp(s1, s2) from root.sg1.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="2") from root.test.d1 +select strreplace(s1, "target"=",", "replace"="/", "limit"="1", "offset"="1", "reverse"="true") from root.test.d1 +select regexmatch(s1, "regex"="\d+\.\d+\.\d+\.\d+", "group"="0") from root.test.d1 +select regexreplace(s1, "regex"="192\.168\.0\.(\d+)", "replace"="cluster-$1", "limit"="1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="-1") from root.test.d1 +select regexsplit(s1, "regex"=",", "index"="3") from root.test.d1 +``` + +### 7.5 数据类型转换函数 + +更多见文档[Data Type Conversion Function](./Operator-and-Expression.md#数据类型转换函数) + +```sql +SELECT cast(s1 as INT32) from root.sg +``` + +### 7.6 常序列生成函数 + +更多见文档[Constant Timeseries Generating Functions](./Operator-and-Expression.md#常序列生成函数) + +```sql +select s1, s2, const(s1, 'value'='1024', 'type'='INT64'), pi(s2), e(s1, s2) from root.sg1.d1; +``` + +### 7.7 选择函数 + +更多见文档[Selector Functions](./Operator-and-Expression.md#选择函数) + +```sql +select s1, top_k(s1, 'k'='2'), bottom_k(s1, 'k'='2') from root.sg1.d2 where time > 2020-12-10T20:36:15.530+08:00; +``` + +### 7.8 区间查询函数 + +更多见文档[Continuous Interval Functions](./Operator-and-Expression.md#区间查询函数) + +```sql +select s1, zero_count(s1), non_zero_count(s2), zero_duration(s3), non_zero_duration(s4) from root.sg.d2; +``` + +### 7.9 趋势计算函数 + +更多见文档[Variation Trend Calculation Functions](./Operator-and-Expression.md#趋势计算函数) + +```sql +select s1, time_difference(s1), difference(s1), non_negative_difference(s1), derivative(s1), non_negative_derivative(s1) from root.sg1.d1 limit 5 offset 1000; + +SELECT DIFF(s1), DIFF(s2) from root.test; +SELECT DIFF(s1, 'ignoreNull'='false'), DIFF(s2, 'ignoreNull'='false') from root.test; +``` + +### 7.10 采样函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#采样函数)。 +### 7.11 时间序列处理函数 + +更多见文档[Sample Functions](./Operator-and-Expression.md#时间序列处理函数)。 + +```sql +select equal_size_bucket_random_sample(temperature,'proportion'='0.1') as random_sample from root.ln.wf01.wt01; +select equal_size_bucket_agg_sample(temperature, 'type'='avg','proportion'='0.1') as agg_avg, equal_size_bucket_agg_sample(temperature, 'type'='max','proportion'='0.1') as agg_max, equal_size_bucket_agg_sample(temperature,'type'='min','proportion'='0.1') as agg_min, equal_size_bucket_agg_sample(temperature, 'type'='sum','proportion'='0.1') as agg_sum, equal_size_bucket_agg_sample(temperature, 'type'='extreme','proportion'='0.1') as agg_extreme, equal_size_bucket_agg_sample(temperature, 'type'='variance','proportion'='0.1') as agg_variance from root.ln.wf01.wt01; +select equal_size_bucket_m4_sample(temperature, 'proportion'='0.1') as M4_sample from root.ln.wf01.wt01; +select equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='avg', 'number'='2') as outlier_avg_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='stendis', 'number'='2') as outlier_stendis_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='cos', 'number'='2') as outlier_cos_sample, equal_size_bucket_outlier_sample(temperature, 'proportion'='0.1', 'type'='prenextdis', 'number'='2') as outlier_prenextdis_sample from root.ln.wf01.wt01; + +select M4(s1,'timeInterval'='25','displayWindowBegin'='0','displayWindowEnd'='100') from root.vehicle.d1 +select M4(s1,'windowSize'='10') from root.vehicle.d1 +``` + +### 7.12 时间序列处理函数 + +更多见文档[Time-Series](./Operator-and-Expression.md#时间序列处理函数) + +```sql +select change_points(s1), change_points(s2), change_points(s3), change_points(s4), change_points(s5), change_points(s6) from root.testChangePoints.d1 +``` + +## 8. 数据质量函数库 + +更多见文档[UDF-Libraries](../SQL-Manual/UDF-Libraries.md) + +### 8.1 数据质量 + +更多见文档[Data-Quality](../SQL-Manual/UDF-Libraries.md#数据质量) + +```sql +# Completeness +select completeness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select completeness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Consistency +select consistency(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select consistency(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Timeliness +select timeliness(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select timeliness(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Validity +select Validity(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 +select Validity(s1,"window"="15") from root.test.d1 where time <= 2020-01-01 00:01:00 + +# Accuracy +select Accuracy(t1,t2,t3,m1,m2,m3) from root.test +``` + +### 8.2 数据画像 + +更多见文档[Data-Profiling](../SQL-Manual/UDF-Libraries.md#数据画像) + +```sql +# ACF +select acf(s1) from root.test.d1 where time <= 2020-01-01 00:00:05 + +# Distinct +select distinct(s2) from root.test.d2 + +# Histogram +select histogram(s1,"min"="1","max"="20","count"="10") from root.test.d1 + +# Integral +select integral(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 +select integral(s1, "unit"="1m") from root.test.d1 where time <= 2020-01-01 00:00:10 + +# IntegralAvg +select integralavg(s1) from root.test.d1 where time <= 2020-01-01 00:00:10 + +# Mad +select mad(s0) from root.test +select mad(s0, "error"="0.01") from root.test + +# Median +select median(s0, "error"="0.01") from root.test + +# MinMax +select minmax(s1) from root.test + +# Mode +select mode(s2) from root.test.d2 + +# MvAvg +select mvavg(s1, "window"="3") from root.test + +# PACF +select pacf(s1, "lag"="5") from root.test + +# Percentile +select percentile(s0, "rank"="0.2", "error"="0.01") from root.test + +# Quantile +select quantile(s0, "rank"="0.2", "K"="800") from root.test + +# Period +select period(s1) from root.test.d3 + +# QLB +select QLB(s1) from root.test.d1 + +# Resample +select resample(s1,'every'='5m','interp'='linear') from root.test.d1 +select resample(s1,'every'='30m','aggr'='first') from root.test.d1 +select resample(s1,'every'='30m','start'='2021-03-06 15:00:00') from root.test.d1 + +# Sample +select sample(s1,'method'='reservoir','k'='5') from root.test.d1 +select sample(s1,'method'='isometric','k'='5') from root.test.d1 + +# Segment +select segment(s1, "error"="0.1") from root.test + +# Skew +select skew(s1) from root.test.d1 + +# Spline +select spline(s1, "points"="151") from root.test + +# Spread +select spread(s1) from root.test.d1 where time <= 2020-01-01 00:00:30 + +# Stddev +select stddev(s1) from root.test.d1 + +# ZScore +select zscore(s1) from root.test +``` + +### 8.3 异常检测 + +更多见文档[Anomaly-Detection](../SQL-Manual/UDF-Libraries.md#异常检测) + +```sql +# IQR +select iqr(s1) from root.test + +# KSigma +select ksigma(s1,"k"="1.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# LOF +select lof(s1,s2) from root.test.d1 where time<1000 +select lof(s1, "method"="series") from root.test.d1 where time<1000 + +# MissDetect +select missdetect(s2,'minlen'='10') from root.test.d2 + +# Range +select range(s1,"lower_bound"="101.0","upper_bound"="125.0") from root.test.d1 where time <= 2020-01-01 00:00:30 + +# TwoSidedFilter +select TwoSidedFilter(s0, 'len'='5', 'threshold'='0.3') from root.test + +# Outlier +select outlier(s1,"r"="5.0","k"="4","w"="10","s"="5") from root.test + +# MasterTrain +select MasterTrain(lo,la,m_lo,m_la,'p'='3','eta'='1.0') from root.test + +# MasterDetect +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='repair','p'='3','k'='3','eta'='1.0') from root.test +select MasterDetect(lo,la,m_lo,m_la,model,'output_type'='anomaly','p'='3','k'='3','eta'='1.0') from root.test +``` + +### 8.4 频域分析 + +更多见文档[Frequency-Domain](../SQL-Manual/UDF-Libraries.md#频域分析) + +```sql +# Conv +select conv(s1,s2) from root.test.d2 + +# Deconv +select deconv(s3,s2) from root.test.d2 +select deconv(s3,s2,'result'='remainder') from root.test.d2 + +# DWT +select dwt(s1,"method"="haar") from root.test.d1 + +# FFT +select fft(s1) from root.test.d1 +select fft(s1, 'result'='real', 'compress'='0.99'), fft(s1, 'result'='imag','compress'='0.99') from root.test.d1 + +# HighPass +select highpass(s1,'wpass'='0.45') from root.test.d1 + +# IFFT +select ifft(re, im, 'interval'='1m', 'start'='2021-01-01 00:00:00') from root.test.d1 + +# LowPass +select lowpass(s1,'wpass'='0.45') from root.test.d1 + +# Envelope +select envelope(s1) from root.test.d1 +``` + +### 8.5 数据匹配 + +更多见文档[Data-Matching](../SQL-Manual/UDF-Libraries.md#数据匹配) + +```sql +# Cov +select cov(s1,s2) from root.test.d2 + +# DTW +select dtw(s1,s2) from root.test.d2 + +# Pearson +select pearson(s1,s2) from root.test.d2 + +# PtnSym +select ptnsym(s4, 'window'='5', 'threshold'='0') from root.test.d1 + +# XCorr +select xcorr(s1, s2) from root.test.d1 where time <= 2020-01-01 00:00:05 +``` + +### 8.6 数据修复 + +更多见文档[Data-Repairing](../SQL-Manual/UDF-Libraries.md#数据修复) + +```sql +# TimestampRepair +select timestamprepair(s1,'interval'='10000') from root.test.d2 +select timestamprepair(s1) from root.test.d2 + +# ValueFill +select valuefill(s1) from root.test.d2 +select valuefill(s1,"method"="previous") from root.test.d2 + +# ValueRepair +select valuerepair(s1) from root.test.d2 +select valuerepair(s1,'method'='LsGreedy') from root.test.d2 + +# MasterRepair +select MasterRepair(t1,t2,t3,m1,m2,m3) from root.test + +# SeasonalRepair +select seasonalrepair(s1,'period'=3,'k'=2) from root.test.d2 +select seasonalrepair(s1,'method'='improved','period'=3) from root.test.d2 +``` + +### 8.7 序列发现 + +更多见文档[Series-Discovery](../SQL-Manual/UDF-Libraries.md#序列发现) + +```sql +# ConsecutiveSequences +select consecutivesequences(s1,s2,'gap'='5m') from root.test.d1 +select consecutivesequences(s1,s2) from root.test.d1 + +# ConsecutiveWindows +select consecutivewindows(s1,s2,'length'='10m') from root.test.d1 +``` + +### 8.8 机器学习 + +更多见文档[Machine-Learning](../SQL-Manual/UDF-Libraries.md#机器学习) + +```sql +# AR +select ar(s0,"p"="2") from root.test.d0 + +# Representation +select representation(s0,"tb"="3","vb"="2") from root.test.d0 + +# RM +select rm(s0, s1,"tb"="3","vb"="2") from root.test.d0 +``` + +## 9. Lambda 表达式 + +更多见文档[Lambda](./Operator-and-Expression.md#lambda-表达式) + +```sql +select jexl(temperature, 'expr'='x -> {x + x}') as jexl1, jexl(temperature, 'expr'='x -> {x * 3}') as jexl2, jexl(temperature, 'expr'='x -> {x * x}') as jexl3, jexl(temperature, 'expr'='x -> {multiply(x, 100)}') as jexl4, jexl(temperature, st, 'expr'='(x, y) -> {x + y}') as jexl5, jexl(temperature, st, str, 'expr'='(x, y, z) -> {x + y + z}') as jexl6 from root.ln.wf01.wt01;``` +``` + +## 10. 条件表达式 + +更多见文档[Conditional Expressions](./Operator-and-Expression.md#条件表达式) + +```sql +select T, P, case +when 1000=1050 then "bad temperature" +when P<=1000000 or P>=1100000 then "bad pressure" +end as `result` +from root.test1 + +select str, case +when str like "%cc%" then "has cc" +when str like "%dd%" then "has dd" +else "no cc and dd" end as `result` +from root.test2 + +select +count(case when x<=1 then 1 end) as `(-∞,1]`, +count(case when 1 +[RESAMPLE + [EVERY ] + [BOUNDARY ] + [RANGE [, end_time_offset]] +] +[TIMEOUT POLICY BLOCKED|DISCARD] +BEGIN + SELECT CLAUSE + INTO CLAUSE + FROM CLAUSE + [WHERE CLAUSE] + [GROUP BY([, ]) [, level = ]] + [HAVING CLAUSE] + [FILL ({PREVIOUS | LINEAR | constant} (, interval=DURATION_LITERAL)?)] + [LIMIT rowLimit OFFSET rowOffset] + [ALIGN BY DEVICE] +END +``` + +#### 配置连续查询执行的周期性间隔 +```sql +CREATE CONTINUOUS QUERY cq1 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询的时间窗口大小 +``` +CREATE CONTINUOUS QUERY cq2 + +RESAMPLE RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + +END + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 同时配置连续查询执行的周期性间隔和时间窗口大小 +```sql +CREATE CONTINUOUS QUERY cq3 + +RESAMPLE EVERY 20s RANGE 40s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 配置连续查询每次查询执行时间窗口的结束时间 +```sql +CREATE CONTINUOUS QUERY cq4 + +RESAMPLE EVERY 20s RANGE 40s, 20s + +BEGIN + + SELECT max_value(temperature) + + INTO root.ln.wf02.wt02(temperature_max), root.ln.wf02.wt01(temperature_max), root.ln.wf01.wt02(temperature_max), root.ln.wf01.wt01(temperature_max) + + FROM root.ln.*.* + + GROUP BY(10s) + + FILL(100.0) + +END + + + +\> SELECT temperature_max from root.ln.*.*; +``` +#### 没有GROUP BY TIME子句的连续查询 +```sql +CREATE CONTINUOUS QUERY cq5 + +RESAMPLE EVERY 20s + +BEGIN + + SELECT temperature + 1 + + INTO root.precalculated_sg.::(temperature) + + FROM root.ln.*.* + + align by device + +END + + + +\> SELECT temperature from root.precalculated_sg.*.* align by device; +``` +### 11.2 连续查询的管理 + +#### 查询系统已有的连续查询 + +展示集群中所有的已注册的连续查询 +```sql +SHOW (CONTINUOUS QUERIES | CQS) +``` +```sql +SHOW CONTINUOUS QUERIES; +``` +#### 删除已有的连续查询 + +删除指定的名为cq_id的连续查询: + +```sql +DROP (CONTINUOUS QUERY | CQ) +``` +```sql +DROP CONTINUOUS QUERY s1_count_cq; +``` +#### 作为子查询的替代品 + +1. 创建一个连续查询 +```sql +CREATE CQ s1_count_cq + +BEGIN + +​ SELECT count(s1) + +​ INTO root.sg_count.d.count_s1 + +​ FROM root.sg.d + +​ GROUP BY(30m) + +END +``` +1. 查询连续查询的结果 +```sql +SELECT avg(count_s1) from root.sg_count.d; +``` +## 12. 用户自定义函数 + +### 12.1 UDFParameters +```sql +SELECT UDF(s1, s2, 'key1'='iotdb', 'key2'='123.45') FROM root.sg.d; +``` +### 12.2 UDF 注册 + +```sql +CREATE FUNCTION AS (USING URI URI-STRING)? +``` + +#### 不指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' +``` +#### 指定URI +```sql +CREATE FUNCTION example AS 'org.apache.iotdb.udf.UDTFExample' USING URI 'http://jar/example.jar' +``` +### 12.3 UDF 卸载 + +```sql +DROP FUNCTION +``` +```sql +DROP FUNCTION example +``` +### 12.4 UDF 查询 + +#### 带自定义输入参数的查询 +```sql +SELECT example(s1, 'key1'='value1', 'key2'='value2'), example(*, 'key3'='value3') FROM root.sg.d1; +``` +```sql +SELECT example(s1, s2, 'key1'='value1', 'key2'='value2') FROM root.sg.d1; +``` +#### 与其他查询的嵌套查询 +```sql +SELECT s1, s2, example(s1, s2) FROM root.sg.d1; + +SELECT *, example(*) FROM root.sg.d1 DISABLE ALIGN; + +SELECT s1 * example(* / s1 + s2) FROM root.sg.d1; + +SELECT s1, s2, s1 + example(s1, s2), s1 - example(s1 + example(s1, s2) / s2) FROM root.sg.d1; +``` +### 12.5 查看所有注册的 UDF +```sql +SHOW FUNCTIONS +``` +## 13. 权限管理 + +### 13.1 用户与角色相关 + +- 创建用户(需 MANAGE_USER 权限) + + +```SQL +CREATE USER +eg: CREATE USER user1 'passwd' +``` + +- 删除用户 (需 MANEGE_USER 权限) + + +```SQL +DROP USER +eg: DROP USER user1 +``` + +- 创建角色 (需 MANAGE_ROLE 权限) + +```SQL +CREATE ROLE +eg: CREATE ROLE role1 +``` + +- 删除角色 (需 MANAGE_ROLE 权限) + + +```SQL +DROP ROLE +eg: DROP ROLE role1 +``` + +- 赋予用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +GRANT ROLE TO +eg: GRANT ROLE admin TO user1 +``` + +- 移除用户角色 (需 MANAGE_ROLE 权限) + + +```SQL +REVOKE ROLE FROM +eg: REVOKE ROLE admin FROM user1 +``` + +- 列出所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER +``` + +- 列出所有角色 (需 MANAGE_ROLE 权限) + +```SQL +LIST ROLE +``` + +- 列出指定角色下所有用户 (需 MANEGE_USER 权限) + +```SQL +LIST USER OF ROLE +eg: LIST USER OF ROLE roleuser +``` + +- 列出指定用户下所有角色 + +用户可以列出自己的角色,但列出其他用户的角色需要拥有 MANAGE_ROLE 权限。 + +```SQL +LIST ROLE OF USER +eg: LIST ROLE OF USER tempuser +``` + +- 列出用户所有权限 + +用户可以列出自己的权限信息,但列出其他用户的权限需要拥有 MANAGE_USER 权限。 + +```SQL +LIST PRIVILEGES OF USER ; +eg: LIST PRIVILEGES OF USER tempuser; + +``` + +- 列出角色所有权限 + +用户可以列出自己具有的角色的权限信息,列出其他角色的权限需要有 MANAGE_ROLE 权限。 + +```SQL +LIST PRIVILEGES OF ROLE ; +eg: LIST PRIVILEGES OF ROLE actor; +``` + +- 更新密码 + +用户可以更新自己的密码,但更新其他用户密码需要具备MANAGE_USER 权限。 + +```SQL +ALTER USER SET PASSWORD ; +eg: ALTER USER tempuser SET PASSWORD 'newpwd'; +``` + +### 13.2 授权与取消授权 + +用户使用授权语句对赋予其他用户权限,语法如下: + +```SQL +GRANT ON TO ROLE/USER [WITH GRANT OPTION]; +eg: GRANT READ ON root.** TO ROLE role1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.** TO USER user1; +eg: GRANT READ_DATA, WRITE_DATA ON root.t1.**,root.t2.** TO USER user1; +eg: GRANT MANAGE_ROLE ON root.** TO USER user1 WITH GRANT OPTION; +eg: GRANT ALL ON root.** TO USER user1 WITH GRANT OPTION; +``` + +用户使用取消授权语句可以将其他的权限取消,语法如下: + +```SQL +REVOKE ON FROM ROLE/USER ; +eg: REVOKE READ ON root.** FROM ROLE role1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.** FROM USER user1; +eg: REVOKE READ_DATA, WRITE_DATA ON root.t1.**, root.t2.** FROM USER user1; +eg: REVOKE MANAGE_ROLE ON root.** FROM USER user1; +eg: REVOKE ALL ON ROOT.** FROM USER user1; +``` +