Skip to content

Commit

Permalink
GitBook: [master] 290 pages modified
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan-Wei authored and gitbook-bot committed May 7, 2021
1 parent 396aa1d commit 95d7721
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
---
description: 支持批
---

# LIMIT子句

`LIMIT`子句限制`SELECT`语句返回的行数。通常,此子句与ORDER BY结合使用以确保结果是确定的。

下面的示例选择`Orders`表中的前3行。

```text
SELECT *
FROM Orders
ORDER BY orderTime
LIMIT 3
```

Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
---
description: 支持批、流
---

# ORDER BY子句

`ORDER BY`子句使结果行根据指定的表达式进行排序。如果根据最左边的表达式两行相等,则根据下一个表达式对它们进行比较,依此类推。如果根据所有指定的表达式,它们是相等的,则以依赖于实现的顺序返回它们。

在流模式下运行时,表的主要排序顺序必须在[时间属性](https://ci.apache.org/projects/flink/flink-docs-release-1.13/docs/dev/table/concepts/time_attributes/)上升序。后续所有的排序可以自由选择。但是在批处理模式下没有此限制。

```text
SELECT *
FROM Orders
ORDER BY order_time, order_id
```

Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
---
description: 支持批、流
---

# SELECT DISTINCT

如果指定了SELECT DISTINCT,则从结果集中删除所有重复的行\(每组重复的行保留一行\)

```text
SELECT DISTINCT id FROM Orders
```

对于流查询,计算查询结果所需的状态可能会无限增长。状态大小取决于不同行的数量。您可以为查询配置提供适当的状态生存时间(TTL),以防止状态大小过大。请注意,这可能会影响查询结果的正确性。有关详细信息,请参见[查询配置](https://ci.apache.org/projects/flink/flink-docs-release-1.13/docs/dev/table/config/#table-exec-state-ttl)

112 changes: 112 additions & 0 deletions 05-ying-yong-kai-fa/04-table-api-and-sql/sql/cha-xun/set-operations.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,114 @@
---
description: 支持批、流
---

# Set Operations

## UNION

`UNION``UNION ALL`返回在任意一个表中找到的行。`UNION`只取不同的行,而`UNION ALL`不从结果行中删除重复的行。

```sql
Flink SQL> create view t1(s) as values ('c'), ('a'), ('b'), ('b'), ('c');
Flink SQL> create view t2(s) as values ('d'), ('e'), ('a'), ('b'), ('b');

Flink SQL> (SELECT s FROM t1) UNION (SELECT s FROM t2);
+---+
| s|
+---+
| c|
| a|
| b|
| d|
| e|
+---+

Flink SQL> (SELECT s FROM t1) UNION ALL (SELECT s FROM t2);
+---+
| c|
+---+
| c|
| a|
| b|
| b|
| c|
| d|
| e|
| a|
| b|
| b|
+---+
```

## INTERSECT

`INTERSECT``INTERSECT ALL`返回在两个表中找到的行。`INTERSECT`只取不同的行,而`INTERSECT ALL`不从结果行中删除重复的行。

```sql
Flink SQL> (SELECT s FROM t1) INTERSECT (SELECT s FROM t2);
+---+
| s|
+---+
| a|
| b|
+---+

Flink SQL> (SELECT s FROM t1) INTERSECT ALL (SELECT s FROM t2);
+---+
| s|
+---+
| a|
| b|
| b|
+---+
```

## EXCEPT

`EXCEPT``EXCEPT ALL`返回在一个表中找到的行,但在另一个表中没有。`EXCEPT`只取不同的行,而`EXCEPT ALL`不从结果行中删除重复的行。

```sql
Flink SQL> (SELECT s FROM t1) EXCEPT (SELECT s FROM t2);
+---+
| s |
+---+
| c |
+---+

Flink SQL> (SELECT s FROM t1) EXCEPT ALL (SELECT s FROM t2);
+---+
| s |
+---+
| c |
| c |
+---+
```

## IN

如果表达式存在于给定的表子查询中,则返回`true`。子查询表必须由一列组成。此列必须与表达式具有相同的数据类型。

```sql
SELECT user, amount
FROM Orders
WHERE product IN (
SELECT product FROM NewProducts
)
```

优化器将IN条件重写为联接和分组操作。对于流查询,根据不同输入行的数量,计算查询结果所需的状态可能会无限增长。您可以为查询配置提供适当的状态生存时间(TTL),以防止状态大小过大。请注意,这可能会影响查询结果的正确性。有关详细信息,请参见[查询配置](https://ci.apache.org/projects/flink/flink-docs-release-1.13/docs/dev/table/config/#table-exec-state-ttl)

## EXISTS

```sql
SELECT user, amount
FROM Orders
WHERE product EXISTS (
SELECT product FROM NewProducts
)
```

如果子查询返回至少一行,则返回true。仅当该操作可以在联接和组操作中重写时才受支持。

优化器将`EXISTS`操作重写为联接和组操作。对于流查询,根据不同输入行的数量,计算查询结果所需的状态可能会无限增长。您可以为查询配置提供适当的状态生存时间(TTL),以防止状态大小过大。请注意,这可能会影响查询结果的正确性。有关详细信息,请参见[查询配置](https://ci.apache.org/projects/flink/flink-docs-release-1.13/docs/dev/table/config/#table-exec-state-ttl)

28 changes: 28 additions & 0 deletions 05-ying-yong-kai-fa/04-table-api-and-sql/sql/cha-xun/with-zi-ju.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
---
description: 支持批、流
---

# WITH子句

`WITH`提供了一种编写用于较大查询的辅助语句的方法。这些语句(通常称为通用表表达式(CTE))可以认为是定义仅针对一个查询存在的临时视图。

`WITH`语句的语法为:

```sql
WITH <with_item_definition> [ , ... ]
SELECT ... FROM ...;

<with_item_defintion>:
with_item_name (column_name[, ...n]) AS ( <select_query> )
```

下面的示例定义一个公用表表达式`orders_with_total`,并在`GROUP BY`查询中使用它。

```sql
WITH orders_with_total AS (
SELECT order_id, price + tax AS total
FROM Orders
)
SELECT order_id, SUM(total)
FROM orders_with_total
GROUP BY order_id;
```

0 comments on commit 95d7721

Please sign in to comment.