这样做:
WITH innermost AS (SELECT 2) SELECT * FROM innermost UNION SELECT 3;
我得到这个:
?column? ---------- 2 3
这样做:
WITH outmost AS ( (WITH innermost AS (SELECT 2) SELECT * FROM innermost) ) SELECT * FROM outmost;
结果:
?column? ---------- 2
这也可以:
WITH outmost AS ( SELECT 1 UNION (WITH innermost AS (SELECT 2) SELECT * FROM innermost) ) SELECT * FROM outmost;
我得到这个:
?column? ---------- 1 2
但这不行:
WITH outmost AS ( SELECT 1 UNION (WITH innermost as (SELECT 2) SELECT * FROM innermost UNION SELECT 3) ) SELECT * FROM outmost;
结果:
ERROR: relation "innermost" does not exist LINE 4: SELECT * FROM innermost
对于我的思维方式,最后一个应该成功,还有一个应该失败。我看不到图案。有没有一些一般规则,可以使我预测嵌套CTE和UNION的组合将会或不会起作用?