提交 9b02278f 编写于 作者: D dingbo8128

fix sql error

上级 d5c72555
...@@ -12,11 +12,11 @@ This section introduces the major features, competitive advantages and typical u ...@@ -12,11 +12,11 @@ This section introduces the major features, competitive advantages and typical u
The major features are listed below: The major features are listed below:
1. Data In 1. Data In
- Supports [using SQL to insert](../data-in/insert-data). - Supports [using SQL to insert](../programming/insert/).
- Supports [Telegraf](../data-in/telegraf/). - Supports [Telegraf](../data-in/telegraf/).
- Supports [Prometheus](../data-in/prometheus/). - Supports [Prometheus](../data-in/prometheus/).
2. Data Out 2. Data Out
- Supports standard [SQL](../data-out/query-data/), including nested query. - Supports standard [SQL](../programming/query/), including nested query.
- Supports exporting data via tool [taosDump](../data-out/taosdump/). - Supports exporting data via tool [taosDump](../data-out/taosdump/).
- Supports writing data to [Prometheus](../data-out/prometheus/). - Supports writing data to [Prometheus](../data-out/prometheus/).
- Supports exporting data via [data subscription](../data-subscription/). - Supports exporting data via [data subscription](../data-subscription/).
......
...@@ -14,26 +14,26 @@ Here are some brief examples for `INSET` statement. You can execute these statem ...@@ -14,26 +14,26 @@ Here are some brief examples for `INSET` statement. You can execute these statem
### Insert Single Row ### Insert Single Row
The below SQL statement is used to insert one row into table "d1001". The below SQL statement is used to insert one row into table "d101".
```sql ```sql
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31); INSERT INTO d101 VALUES (1538548685000, 10.3, 219, 0.31);
``` ```
### Insert Multiple Rows ### Insert Multiple Rows
Multiple rows can be inserted in a single SQL statement. The example below inserts 2 rows into table "d1001". Multiple rows can be inserted in a single SQL statement. The example below inserts 2 rows into table "d101".
```sql ```sql
INSERT INTO d1001 VALUES (1538548684000, 10.2, 220, 0.23) (1538548696650, 10.3, 218, 0.25); INSERT INTO d101 VALUES (1538548684000, 10.2, 220, 0.23) (1538548696650, 10.3, 218, 0.25);
``` ```
### Insert into Multiple Tables ### Insert into Multiple Tables
Data can be inserted into multiple tables in the same SQL statement. The example below inserts 2 rows into table "d1001" and 1 row into table "d1002". Data can be inserted into multiple tables in the same SQL statement. The example below inserts 2 rows into table "d101" and 1 row into table "d1002".
```sql ```sql
INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, 218, 0.33) d1002 VALUES (1538548696800, 12.3, 221, 0.31); INSERT INTO d101 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6, 218, 0.33) d1002 VALUES (1538548696800, 12.3, 221, 0.31);
``` ```
For more details about `INSERT` please refer to [INSERT](https://docs.tdengine.com/cloud/taos-sql/insert). For more details about `INSERT` please refer to [INSERT](https://docs.tdengine.com/cloud/taos-sql/insert).
......
...@@ -22,12 +22,11 @@ SQL is used by TDengine as its query language. Application programs can send SQL ...@@ -22,12 +22,11 @@ SQL is used by TDengine as its query language. Application programs can send SQL
For example, the SQL statement below can be executed in TDengine CLI `taos` to select records with voltage greater than 215 and limit the output to only 2 rows. For example, the SQL statement below can be executed in TDengine CLI `taos` to select records with voltage greater than 215 and limit the output to only 2 rows.
```sql ```sql title="SQL"
select * from d1001 where voltage > 215 order by ts desc limit 2; select * from test.d101 where voltage > 100 order by ts desc limit 2;
``` ```
```title=Output ```txt title="output"
taos> select * from d1001 where voltage > 215 order by ts desc limit 2;
ts | current | voltage | phase | ts | current | voltage | phase |
====================================================================================== ======================================================================================
2018-10-03 14:38:16.800 | 12.30000 | 221 | 0.31000 | 2018-10-03 14:38:16.800 | 12.30000 | 221 | 0.31000 |
...@@ -47,23 +46,36 @@ In summary, records across subtables can be aggregated by a simple query on thei ...@@ -47,23 +46,36 @@ In summary, records across subtables can be aggregated by a simple query on thei
### Example 1 ### Example 1
In TDengine CLI `taos`, use the SQL below to get the average voltage of all the meters in California grouped by location. In [TDengine CLI](../tool/cli) `taos`, use the SQL below to get the average voltage of all the meters in California grouped by location.
```sql title="SQL"
SELECT AVG(voltage) FROM test.meters GROUP BY location;
``` ```
taos> SELECT AVG(voltage) FROM meters GROUP BY location;
avg(voltage) | location | ```txt title="output"
============================================================= avg(voltage) |
222.000000000 | California.LosAngeles | ============================
219.200000000 | California.SanFrancisco | 109.507000000 |
Query OK, 2 row(s) in set (0.002136s) 109.507000000 |
109.507000000 |
109.507000000 |
109.507000000 |
109.507000000 |
109.507000000 |
109.507000000 |
109.507000000 |
109.507000000 |
Query OK, 10 row(s) in set
``` ```
### Example 2 ### Example 2
In TDengine CLI `taos`, use the SQL below to get the number of rows and the maximum current in the past 24 hours from meters whose groupId is 2. In TDengine CLI `taos`, use the SQL below to get the number of rows and the maximum current in the past 24 hours from meters whose groupId is 2.
```sql title="SQL"
SELECT count(*), max(current) FROM test.meters where groupId = 2 and ts > now - 24h;
``` ```
taos> SELECT count(*), max(current) FROM meters where groupId = 2 and ts > now - 24h; ```txt title="output"
count(*) | max(current) | count(*) | max(current) |
================================== ==================================
5 | 13.4 | 5 | 13.4 |
...@@ -76,41 +88,50 @@ Join queries are only allowed between subtables of the same STable. In [Select]( ...@@ -76,41 +88,50 @@ Join queries are only allowed between subtables of the same STable. In [Select](
In IoT use cases, down sampling is widely used to aggregate data by time range. The `INTERVAL` keyword in TDengine can be used to simplify the query by time window. For example, the SQL statement below can be used to get the sum of current every 10 seconds from meters table d1001. In IoT use cases, down sampling is widely used to aggregate data by time range. The `INTERVAL` keyword in TDengine can be used to simplify the query by time window. For example, the SQL statement below can be used to get the sum of current every 10 seconds from meters table d1001.
```sql title="SQL"
SELECT _wstart, sum(current) FROM test.d101 INTERVAL(10s) limit 3;
``` ```
taos> SELECT sum(current) FROM d1001 INTERVAL(10s);
ts | sum(current) | ```txt title="output"
_wstart | sum(current) |
====================================================== ======================================================
2018-10-03 14:38:00.000 | 10.300000191 | 2017-07-14 10:40:00.000 | 9.920000076 |
2018-10-03 14:38:10.000 | 24.900000572 | 2017-07-14 10:55:00.000 | 9.840000153 |
Query OK, 2 row(s) in set (0.000883s) 2017-07-14 11:10:00.000 | 9.840000153 |
Query OK, 3 row(s) in set
``` ```
Down sampling can also be used for STable. For example, the below SQL statement can be used to get the sum of current from all meters in California. Down sampling can also be used for STable. For example, the below SQL statement can be used to get the sum of current from all meters in California.
```sql title="SQL"
SELECT _wstart, SUM(current) FROM test.meters where location like "California%" INTERVAL(1s) limit 5;
``` ```
taos> SELECT SUM(current) FROM meters where location like "California%" INTERVAL(1s); ```txt title="output"
ts | sum(current) | _wstart | sum(current) |
====================================================== ======================================================
2018-10-03 14:38:04.000 | 10.199999809 | 2017-07-14 10:40:00.000 | 9920.000076294 |
2018-10-03 14:38:05.000 | 32.900000572 | 2017-07-14 10:55:00.000 | 9840.000152588 |
2018-10-03 14:38:06.000 | 11.500000000 | 2017-07-14 11:10:00.000 | 9840.000152588 |
2018-10-03 14:38:15.000 | 12.600000381 | 2017-07-14 11:25:00.000 | 10119.999885559 |
2018-10-03 14:38:16.000 | 36.000000000 | 2017-07-14 11:40:00.000 | 9800.000190735 |
Query OK, 5 row(s) in set (0.001538s) Query OK, 5 row(s) in set
``` ```
Down sampling also supports time offset. For example, the below SQL statement can be used to get the sum of current from all meters but each time window must start at the boundary of 500 milliseconds. Down sampling also supports time offset. For example, the below SQL statement can be used to get the sum of current from all meters but each time window must start at the boundary of 500 milliseconds.
```sql title="SQL"
SELECT _wstart, SUM(current) FROM test.meters INTERVAL(1s, 500a) limit 5;
``` ```
taos> SELECT SUM(current) FROM meters INTERVAL(1s, 500a);
ts | sum(current) | ```txt title="output"
_wstart | sum(current) |
====================================================== ======================================================
2018-10-03 14:38:04.500 | 11.189999809 | 2017-07-14 10:39:59.500 | 9920.000076294 |
2018-10-03 14:38:05.500 | 31.900000572 | 2017-07-14 10:54:59.500 | 9840.000152588 |
2018-10-03 14:38:06.500 | 11.600000000 | 2017-07-14 11:09:59.500 | 9840.000152588 |
2018-10-03 14:38:15.500 | 12.300000381 | 2017-07-14 11:24:59.500 | 10119.999885559 |
2018-10-03 14:38:16.500 | 35.000000000 | 2017-07-14 11:39:59.500 | 9800.000190735 |
Query OK, 5 row(s) in set (0.001521s) Query OK, 5 row(s) in set
``` ```
In many use cases, it's hard to align the timestamp of the data collected by each collection point. However, a lot of algorithms like FFT require the data to be aligned with same time interval and application programs have to handle this by themselves. In TDengine, it's easy to achieve the alignment using down sampling. In many use cases, it's hard to align the timestamp of the data collected by each collection point. However, a lot of algorithms like FFT require the data to be aligned with same time interval and application programs have to handle this by themselves. In TDengine, it's easy to achieve the alignment using down sampling.
......
--- ---
sidebar_label: Developer Guide sidebar_label: Programming
title: Developer Guide title: Programming
description: The most fundamental knowledge about programming with TDengine. description: The most fundamental knowledge about programming with TDengine.
--- ---
......
...@@ -33,19 +33,17 @@ It is common that smart electrical meter systems for businesses generate million ...@@ -33,19 +33,17 @@ It is common that smart electrical meter systems for businesses generate million
### Create a Database for Raw Data ### Create a Database for Raw Data
A database including one supertable and four subtables is created as follows: Create database `power` using explore in cloud console.
```sql Then create four subtables as follows:
DROP DATABASE IF EXISTS power;
CREATE DATABASE power;
USE power;
CREATE STABLE meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int); ```sql
CREATE STABLE power.meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);
CREATE TABLE d1001 USING meters TAGS ("California.SanFrancisco", 2); CREATE TABLE power.d101 USING meters TAGS ("California.SanFrancisco", 2);
CREATE TABLE d1002 USING meters TAGS ("California.SanFrancisco", 3); CREATE TABLE power.d102 USING meters TAGS ("California.SanFrancisco", 3);
CREATE TABLE d1003 USING meters TAGS ("California.LosAngeles", 2); CREATE TABLE power.d103 USING meters TAGS ("California.LosAngeles", 2);
CREATE TABLE d1004 USING meters TAGS ("California.LosAngeles", 3); CREATE TABLE power.d104 USING meters TAGS ("California.LosAngeles", 3);
``` ```
### Create a Stream ### Create a Stream
...@@ -56,20 +54,23 @@ create stream current_stream into current_stream_output_stb as select _wstart as ...@@ -56,20 +54,23 @@ create stream current_stream into current_stream_output_stb as select _wstart as
### Write Data ### Write Data
```sql ```sql
insert into d1001 values("2018-10-03 14:38:05.000", 10.30000, 219, 0.31000); insert into d101 values("2018-10-03 14:38:05.000", 10.30000, 219, 0.31000);
insert into d1001 values("2018-10-03 14:38:15.000", 12.60000, 218, 0.33000); insert into d101 values("2018-10-03 14:38:15.000", 12.60000, 218, 0.33000);
insert into d1001 values("2018-10-03 14:38:16.800", 12.30000, 221, 0.31000); insert into d101 values("2018-10-03 14:38:16.800", 12.30000, 221, 0.31000);
insert into d1002 values("2018-10-03 14:38:16.650", 10.30000, 218, 0.25000); insert into d102 values("2018-10-03 14:38:16.650", 10.30000, 218, 0.25000);
insert into d1003 values("2018-10-03 14:38:05.500", 11.80000, 221, 0.28000); insert into d103 values("2018-10-03 14:38:05.500", 11.80000, 221, 0.28000);
insert into d1003 values("2018-10-03 14:38:16.600", 13.40000, 223, 0.29000); insert into d103 values("2018-10-03 14:38:16.600", 13.40000, 223, 0.29000);
insert into d1004 values("2018-10-03 14:38:05.000", 10.80000, 223, 0.29000); insert into d104 values("2018-10-03 14:38:05.000", 10.80000, 223, 0.29000);
insert into d1004 values("2018-10-03 14:38:06.500", 11.50000, 221, 0.35000); insert into d104 values("2018-10-03 14:38:06.500", 11.50000, 221, 0.35000);
``` ```
### Query the Results ### Query the Results
```sql ```sql title="SQL"
taos> select start, end, max_current from current_stream_output_stb; select start, end, max_current from current_stream_output_stb;
```
```txt title="output"
start | end | max_current | start | end | max_current |
=========================================================================== ===========================================================================
2018-10-03 14:38:05.000 | 2018-10-03 14:38:10.000 | 10.30000 | 2018-10-03 14:38:05.000 | 2018-10-03 14:38:10.000 | 10.30000 |
...@@ -77,6 +78,7 @@ taos> select start, end, max_current from current_stream_output_stb; ...@@ -77,6 +78,7 @@ taos> select start, end, max_current from current_stream_output_stb;
Query OK, 2 rows in database (0.018762s) Query OK, 2 rows in database (0.018762s)
``` ```
## Usage Scenario 2 ## Usage Scenario 2
In this scenario, the active power and reactive power are determined from the data gathered in the previous scenario. The location and name of each meter are concatenated with a period (.) between them, and the data set is partitioned by meter name and written to a new database. In this scenario, the active power and reactive power are determined from the data gathered in the previous scenario. The location and name of each meter are concatenated with a period (.) between them, and the data set is partitioned by meter name and written to a new database.
...@@ -96,17 +98,19 @@ create stream power_stream into power_stream_output_stb as select ts, concat_ws( ...@@ -96,17 +98,19 @@ create stream power_stream into power_stream_output_stb as select ts, concat_ws(
The procedure from the previous scenario is used to write the data. The procedure from the previous scenario is used to write the data.
### Query the Results ### Query the Results
```sql ```sql title="SQL"
taos> select ts, meter_location, active_power, reactive_power from power_stream_output_stb; taos> select ts, meter_location, active_power, reactive_power from power_stream_output_stb;
```
```txt title="output"
ts | meter_location | active_power | reactive_power | ts | meter_location | active_power | reactive_power |
=================================================================================================================== ===================================================================================================================
2018-10-03 14:38:05.000 | California.LosAngeles.d1004 | 2307.834596289 | 688.687331847 | 2018-10-03 14:38:05.000 | California.LosAngeles.d104 | 2307.834596289 | 688.687331847 |
2018-10-03 14:38:06.500 | California.LosAngeles.d1004 | 2387.415754896 | 871.474763418 | 2018-10-03 14:38:06.500 | California.LosAngeles.d104 | 2387.415754896 | 871.474763418 |
2018-10-03 14:38:05.500 | California.LosAngeles.d1003 | 2506.240411679 | 720.680274962 | 2018-10-03 14:38:05.500 | California.LosAngeles.d103 | 2506.240411679 | 720.680274962 |
2018-10-03 14:38:16.600 | California.LosAngeles.d1003 | 2863.424274422 | 854.482390839 | 2018-10-03 14:38:16.600 | California.LosAngeles.d103 | 2863.424274422 | 854.482390839 |
2018-10-03 14:38:05.000 | California.SanFrancisco.d1001 | 2148.178871730 | 688.120784090 | 2018-10-03 14:38:05.000 | California.SanFrancisco.d101 | 2148.178871730 | 688.120784090 |
2018-10-03 14:38:15.000 | California.SanFrancisco.d1001 | 2598.589176205 | 890.081451418 | 2018-10-03 14:38:15.000 | California.SanFrancisco.d101 | 2598.589176205 | 890.081451418 |
2018-10-03 14:38:16.800 | California.SanFrancisco.d1001 | 2588.728381186 | 829.240910475 | 2018-10-03 14:38:16.800 | California.SanFrancisco.d101 | 2588.728381186 | 829.240910475 |
2018-10-03 14:38:16.650 | California.SanFrancisco.d1002 | 2175.595991997 | 555.520860397 | 2018-10-03 14:38:16.650 | California.SanFrancisco.d102 | 2175.595991997 | 555.520860397 |
Query OK, 8 rows in database (0.014753s) Query OK, 8 rows in database (0.014753s)
``` ```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册