ClickHouse/docs/zh/sql-reference/statements/select/with.md
mikepop7 ad62e42229
Documentation improve: Translate [select] section into Chinese to improve readability (#13814)
* translate [select] section into Chinese to improve readability

* trigger ci

Co-authored-by: mikepop7 <>
Co-authored-by: Ivan Blinkov <github@blinkov.ru>
2020-08-17 23:28:11 +03:00

1.6 KiB
Raw Blame History

toc_title
WITH

WITH子句

本节提供对公共表表达式的支持 (CTE),所以结果 WITH 子句可以在其余部分中使用 SELECT 查询。

限制

  1. 不支持递归查询。
  2. 当在section中使用子查询时它的结果应该是只有一行的标量。
  3. Expression的结果在子查询中不可用。

示例1: 使用常量表达式作为 “variable”

WITH '2019-08-01 15:23:00' as ts_upper_bound
SELECT *
FROM hits
WHERE
    EventDate = toDate(ts_upper_bound) AND
    EventTime <= ts_upper_bound

示例2: 从SELECT子句列表中逐出sum(bytes)表达式结果

WITH sum(bytes) as s
SELECT
    formatReadableSize(s),
    table
FROM system.parts
GROUP BY table
ORDER BY s

例3: 使用标量子查询的结果

/* this example would return TOP 10 of most huge tables */
WITH
    (
        SELECT sum(bytes)
        FROM system.parts
        WHERE active
    ) AS total_disk_usage
SELECT
    (sum(bytes) / total_disk_usage) * 100 AS table_disk_usage,
    table
FROM system.parts
GROUP BY table
ORDER BY table_disk_usage DESC
LIMIT 10

例4: 在子查询中重用表达式

作为子查询中表达式使用的当前限制的解决方法,您可以复制它。

WITH ['hello'] AS hello
SELECT
    hello,
    *
FROM
(
    WITH ['hello'] AS hello
    SELECT hello
)
┌─hello─────┬─hello─────┐
│ ['hello'] │ ['hello'] │
└───────────┴───────────┘