【问题标题】:Use result of WITH statement inside a subquery at Clickhouse在 Clickhouse 的子查询中使用 WITH 语句的结果
【发布时间】:2024-05-21 03:15:02
【问题描述】:

以下查询工作正常:

WITH
    (
        SELECT 1
    ) AS test_val
SELECT
    test_val,
    count()
FROM system.tables

┌─test_val─┬─count()─┐
│        1 │      93 │
└──────────┴─────────┘

但如果我用子查询重写:

WITH
    (
        SELECT 1
    ) AS test_val
SELECT *
FROM
(
    SELECT
        test_val,
        count()
    FROM system.tables
)

有一个错误:

Code: 47. DB::Exception: Received from clickhouse-server:9000. DB::Exception: Missing columns: 'test_val' while processing query: 'SELECT test_val, count() FROM system.tables', required columns: 'test_val', source columns: 'total_bytes' ...

(我知道这是无用的查询,我只是在这里发布示例)

如何在子查询中使用test_val

Clickhouse 版本:20.11

【问题讨论】:

    标签: clickhouse


    【解决方案1】:

    发现WITH可以移动到嵌套查询中:

    SELECT *
    FROM
    (
        WITH
        (
            SELECT 1
        ) AS test_val
        SELECT
            test_val,
            count()
        FROM system.tables
    )
    

    【讨论】: