Browse Source

docs: add sql example (#2163)

Signed-off-by: yisaer <disxiaofei@163.com>
Song Gao 1 year ago
parent
commit
4e69110c79
2 changed files with 204 additions and 0 deletions
  1. 96 0
      docs/en_US/sqls/query_language_elements.md
  2. 108 0
      docs/zh_CN/sqls/query_language_elements.md

+ 96 - 0
docs/en_US/sqls/query_language_elements.md

@@ -36,6 +36,12 @@ Specifies that all columns from all input streams in the FROM clause should be r
 
 Select all of fields from source stream.
 
+example:
+
+```sql
+select * from demo;
+```
+
 **\* EXCEPT**
 
 Specify one or more fields to be excluded from the result. It allows excluding one or more specific column names from the query result while still including other columns.
@@ -45,6 +51,12 @@ SELECT * EXCEPT(column_name1, column_name2...)
 FROM stream1
 ```
 
+example:
+
+```sql
+select * except(a,b) from demo;
+```
+
 **\* REPLACE**
 
 Replace specific columns in the result. It allows for the replacement of certain columns in the result by specifying new expressions, while other columns are still included in the output.
@@ -54,6 +66,12 @@ SELECT * REPLACE(expression1 as column_name1, expression2 as column_name2...)
 FROM stream1
 ```
 
+example:
+
+```sql
+select * replace(a+b as c) from demo;
+```
+
 REPLACE and EXCEPT can be used together, but it's important to note that if there is a conflict between these two operations, REPLACE takes precedence. In the following example, the final result will include the column_name1 field.
 
 ```sql
@@ -143,6 +161,12 @@ LEFT JOIN stream2
 ON stream1.column_name = stream2.column_name;
 ```
 
+example:
+
+```sql
+select * from stream1 left join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **RIGHT**
 
 The RIGHT JOIN keyword returns all records from the right stream (stream2), and the matched records from the left stream (stream1). The result is NULL from the left side, when there is no match.
@@ -154,6 +178,12 @@ RIGHT JOIN stream2
 ON stream1.column_name = stream2.column_name;
 ```
 
+example:
+
+```sql
+select * from stream1 right join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **FULL**
 
 The FULL JOIN keyword return all records when there is a match in left (stream1) or right (stream2) table records.
@@ -168,6 +198,12 @@ ON stream1.column_name = stream2.column_name
 WHERE condition;
 ```
 
+example:
+
+```sql
+select * from stream1 full join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **CROSS**
 
 The CROSS JOIN is used to combine each row of the first stream (stream1) with each row of the second stream (stream2). It is also known as the Cartesian join since it returns the Cartesian product of the sets of rows from the joined tables. Let's say if there are **m** rows in stream1, and **n** rows in stream2, then the result of CROSS  JOIN returns **m*n** rows.
@@ -182,6 +218,12 @@ ON stream1.column_name = stream2.column_name
 WHERE condition;
 ```
 
+example:
+
+```sql
+select * from stream1 cross outer join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **source_stream | source_stream_alias**
 
 The input stream name or alias name to be joined.
@@ -206,6 +248,12 @@ WHERE <search_condition>
     { expression { = | < > | ! = | > | > = | < | < = } expression   
 ```
 
+exmaple:
+
+```sql
+select * from demo where a > 10;
+```
+
 ### Arguments
 
 Expression is a constant, function, any combination of column names, constants, and functions connected by an operator or operators.
@@ -218,10 +266,22 @@ Specifies the conditions for the rows returned in the result set for a SELECT st
 
 Combines two conditions and evaluates to TRUE when both of the conditions are TRUE.
 
+example:
+
+```sql
+select * from demo where a > 10 and a < 15;
+```
+
 **OR**
 
 Combines two conditions and evaluates to TRUE when either condition is TRUE.
 
+exmaple:
+
+```sql
+select * from demo where a > 10 or a < 15;
+```
+
 **< predicate >**
 
 Is an expression that returns TRUE or FALSE.
@@ -266,6 +326,12 @@ Is the operator used to test the condition of one expression in (not) within the
 expression [NOT] BETWEEN expression1 AND expression2
 ```
 
+exmaple:
+
+```sql
+select * from demo where a between 10 and 15;
+```
+
 **[NOT] LIKE**
 
 Is the operator used to check if the STRING in the first operand matches a pattern specified by the second operand. Patterns can contain these characters:
@@ -283,6 +349,12 @@ Example:
 a LIKE "string%"
 ```
 
+exmaple:
+
+```sql
+select * from demo where a like "prefix%"
+```
+
 **[NOT] IN**
 
 Is the operator used to test the condition of one expression (not) being part of to the other expression. support these two formats
@@ -338,6 +410,12 @@ FROM stream1
 GROUP BY column_name
 ```
 
+example:
+
+```sql
+select * from demo group by a, countwindow(5);
+```
+
 ### HAVING
 
 The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. Specifies a search condition for a group or an aggregate. HAVING can be used only with the SELECT expression. HAVING is typically used in a GROUP BY clause.
@@ -358,6 +436,12 @@ Specifies the search condition for the group or the aggregate to meet.
 SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3
 ```
 
+example:
+
+```sql
+select * from demo group by countwindow(5) having a > 10;
+```
+
 ## ORDER BY
 
 Order the rows by values of one or more columns.
@@ -370,6 +454,12 @@ ORDER BY column1, column2, ... ASC|DESC
 
 The ORDER BY statement in sql is used to sort the fetched data in either ascending or descending according to one or more columns.
 
+exmaple:
+
+```sql
+select * from demo group by countwindow(5) order by a ASC;
+```
+
 - By default ORDER BY sorts the data in **ascending order.**
 - The keyword DESC is used to sort the data in descending order and the keyword ASC to sort in ascending order.
 
@@ -403,6 +493,12 @@ The case expression evaluates a list of conditions and returns one of multiple p
 
 There are two types of case expression: simple case expression and searched case expression.
 
+exmaple:
+
+```sql
+select * from demo where a > 10 group by countwindow(5) limit 10;
+```
+
 ### Simple Case Expression
 
 The simple case expression compares an expression to a set of simple expressions to determine the result.

+ 108 - 0
docs/zh_CN/sqls/query_language_elements.md

@@ -37,6 +37,12 @@ SELECT
 
 从源流中选择所有字段。
 
+例子:
+
+```sql
+select * from demo;
+```
+
 **\* EXCEPT**
 
 用于指定一个或多个字段,以便从结果中排除这些字段。它允许在查询结果中排除一个或多个特定的列名,而其他列则仍然包含在查询结果中。
@@ -46,6 +52,12 @@ SELECT * EXCEPT(column_name1, column_name2...)
 FROM stream1
 ```
 
+例子:
+
+```sql
+select * except(a,b) from demo;
+```
+
 **\* REPLACE**
 
 用于在SQL查询结果中替换指定列。它允许通过指定新的表达式来替换查询结果中的某些列,而其他列则仍然包含在查询结果中。
@@ -55,6 +67,12 @@ SELECT * REPLACE(expression1 as column_name1, expression2 as column_name2...)
 FROM stream1
 ```
 
+例子:
+
+```sql
+select * replace(a+b as c) from demo;
+```
+
 REPLACE 和 EXCEPT 可以同时使用,但需要注意的是如果这两个操作之间存在冲突,REPLACE 操作具有优先权。比如在下面的例子中,最终的结果包含`column_name1`字段。
 
 ```sql
@@ -144,6 +162,12 @@ LEFT JOIN stream2
 ON stream1.column_name = stream2.column_name;
 ```
 
+例子:
+
+```sql
+select * from stream1 left join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **RIGHT**
 
 JOIN 关键字从右侧流(stream2)返回所有记录,并从左侧流(stream1)返回匹配的记录。 如果没有匹配项,则结果从左侧为 NULL。
@@ -155,6 +179,12 @@ RIGHT JOIN stream2
 ON stream1.column_name = stream2.column_name;
 ```
 
+例子:
+
+```sql
+select * from stream1 right join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **FULL**
 
 当左(stream1)或右(stream2)表记录匹配时,FULL JOIN 关键字返回所有记录。
@@ -169,6 +199,12 @@ ON stream1.column_name = stream2.column_name
 WHERE condition;
 ```
 
+例子:
+
+```sql
+select * from stream1 full join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **CROSS**
 
 CROSS JOIN 用于将第一个流(stream1)的每一行与第二个流(stream2)的每一行组合。 这也称为笛卡尔联接,因为它从联接表返回行集的笛卡尔乘积。 假设在 stream1中有 m 行,在stream2 中有 n 行,那么 CROSS JOIN 的结果将返回 m * n 行。
@@ -183,6 +219,12 @@ ON stream1.column_name = stream2.column_name
 WHERE condition;
 ```
 
+例子:
+
+```sql
+select * from stream1 cross outer join on stream2 stream1.column = stream2.column group by countwindow(5);
+```
+
 **source_stream | source_stream_alias**
 
 要连接的输入流名称或别名。
@@ -207,6 +249,12 @@ WHERE <search_condition>
     { expression { = | < > | ! = | > | > = | < | < = | NOT IN} expression   
 ```
 
+例子:
+
+```sql
+select * from demo where a > 10;
+```
+
 ### 参数
 
 表达式是一个常量、函数、及由一个或多个运算符连接的列名、常量和函数的任意组合。
@@ -219,10 +267,20 @@ WHERE <search_condition>
 
 组合两个条件,当两个条件都为真时计算为真。
 
+例子:
+
+```sql
+select * from demo where a > 10 and a < 15;
+```
+
 **OR**
 
 组合两个条件,当任一条件为真时计算为真。
 
+```sql
+select * from demo where a > 10 or a < 15;
+```
+
 **< predicate >**
 
 返回 TRUE 或 FALSE 的表达式。
@@ -267,6 +325,12 @@ WHERE <search_condition>
 expression [NOT] BETWEEN expression1 AND expression2
 ```
 
+例子:
+
+```sql
+select * from demo where a between 10 and 15;
+```
+
 **[NOT] LIKE**
 
 用于测试字符串是否满足模式。模式可使用以下通配符:
@@ -284,6 +348,12 @@ expression [NOT] LIKE expression1
 a LIKE "string%"
 ```
 
+例子:
+
+```sql
+select * from demo where a like "prefix%"
+```
+
 **[NOT] IN**
 
 用于测试一个表达式是否属于另一个表达式的条件的运算符。
@@ -293,12 +363,22 @@ a LIKE "string%"
   expression [NOT] IN (expression2,...n)
 ```
 
+例子:
+
+```sql
+select * from demo where a in (10,11,12);
+```
+
 *注意*: 支持同时设置多个表达式, 但用户须确保每个表达式返回值为单一值
 
 ```sql
   expression [NOT] IN expression2
 ```
 
+```sql
+select * from demo where a not in (10,11,12);
+```
+
 *注意*: 用户须确保 expression2 的返回值为数组
 
 ```sql
@@ -330,6 +410,12 @@ GROUP BY <group by spec>
 
 指定任何支持 eKuiper 的窗口,有关详细信息,请参阅 [windows](windows.md) 。
 
+例子:
+
+```sql
+select * from demo group by countwindow(2);
+```
+
 **< column_expression >**
 
 执行分组操作的列的表达式或名称。列表达式不能包含在选择列表中定义的列别名。
@@ -340,6 +426,10 @@ FROM stream1
 GROUP BY column_name
 ```
 
+```sql
+select * from demo group by a, countwindow(5);
+```
+
 ### HAVING
 
 指定组或集合的搜索条件。 HAVING 只能与 SELECT 表达式一起使用。 HAVING 通常在 GROUP BY 子句中使用。 如果不使用 GROUP BY,则 HAVING 的行为类似于WHERE 子句。
@@ -360,6 +450,12 @@ GROUP BY column_name
 SELECT temp AS t, name FROM topic/sensor1 WHERE name = "dname" GROUP BY name HAVING count(name) > 3
 ```
 
+例子:
+
+```sql
+select * from demo group by countwindow(5) having a > 10;
+```
+
 ## ORDER BY
 
 按一列或多列的值对行进行排序。
@@ -391,6 +487,12 @@ FROM table_name
 ORDER BY column1, column2, ... ASC|DESC;
 ```
 
+例子:
+
+```sql
+select * from demo group by countwindow(5) order by a ASC;
+```
+
 ## LIMIT
 
 将输出的数据条数进行限制
@@ -399,6 +501,12 @@ ORDER BY column1, column2, ... ASC|DESC;
 LIMIT 1
 ```
 
+例子:
+
+```sql
+select * from demo where a > 10 group by countwindow(5) limit 10;
+```
+
 ## Case 表达式
 
 Case 表达式评估一系列条件,并返回多个可能的结果表达式之一。它允许你在 SQL 语句中使用 IF ... THEN ... ELSE 逻辑,而无需调用过程。