在 Clickhouse 的子查询中使用 WITH 语句的结果

Use result of WITH statement inside a subquery at Clickhouse

以下查询工作正常:

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' ...

(我知道这是无用的查询,我只是post这里举个例子)

如何在子查询中使用 test_val

Clickhouse 版本:20.11

发现WITH可以移入嵌套查询:

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