如何在单个SQL查询中使用多个CTE?
是否可以将多个CTE组合在单个查询中?
我正在寻找这样的结果:
WITH cte1 AS (
...
),
WITH RECURSIVE cte2 AS (
...
),
WITH cte3 AS (
...
)
SELECT ... FROM cte3 WHERE ...
如您所见,我有一个递归的CTE和两个非递归。
Is it possible to combine multiple CTEs in single query?
I am looking for way to get result like this:
WITH cte1 AS (
...
),
WITH RECURSIVE cte2 AS (
...
),
WITH cte3 AS (
...
)
SELECT ... FROM cte3 WHERE ...
As you can see, I have one recursive CTE and two non recursive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在顶部使用 的关键词
。如果您的任何常见表表达式(CTE)是递归(RCTE),则必须在top 上添加关键字
递归
,即使并非所有CTE都是递归:<
大胆的重点是我的。而且,更具洞察力:
再次强调我的。这意味着使用条款的
的顺序是 时使用
递归
键>关键词。顺便说一句,由于
cte1
和cte2
在示例中未引用select
和plainselect
select 命令自己(没有附带效果),它们从未执行过(除非cte3
中引用)。Use the key word
WITH
once at the top. If any of your Common Table Expressions (CTE) are recursive (rCTE) you have to add the keywordRECURSIVE
at the top once also, even if not all CTEs are recursive:The manual:
Bold emphasis mine. And, even more insightful:
Bold emphasis mine again. Meaning that the order of
WITH
clauses is meaningless when theRECURSIVE
key word has been used.BTW, since
cte1
andcte2
in the example are not referenced in the outerSELECT
and are plainSELECT
commands themselves (no collateral effects), they are never executed (unless referenced incte3
).是的。您不必重复
WITH
。您只需使用逗号:And: 仅对字符串和日期常量使用单引号。不要将它们用作列别名。无论如何,它们不允许用于 CTE 名称。
Yes. You don't repeat the
WITH
. You just use a comma:And: Only use single quotes for string and date constants. Don't use them for column aliases. They are not allowed for CTE names anyway.
正如已接受的答案所正确指出的那样,
with
子句每个 CTE 链仅使用一次。不过,为了完整起见,我想补充一下它不会阻止您嵌套 CTE。如果
cte2
使用cte1
,cte3
使用cte2
等,那么CTE之间的依赖链是线性的,并且它是表示为with
,具有 3 个 CTE。相反,如果cte2
不需要cte1
并且两者仅在cte3
中需要,则应考虑将它们嵌套在cte1
的定义下code>cte3(with cte3 as(with cte1 as (...), cte2 as (...) select...)
)。然后,CTE 的语法反映了 CTE 之间的依赖关系树,并从字面上可视化部分数据集的范围,这可以提高可读性并防止范围泄漏错误。并非所有数据库供应商都支持它,但 Postgres 支持。
示例:
As the accepted answer correctly says, the
with
clause is used only once per a CTE chain. However, for sake of completeness, I would like to add it does not stop you from nesting CTEs.If
cte2
usescte1
,cte3
usescte2
etc., then the dependency chain between CTEs is linear and it is expressed aswith
with 3 CTEs. On the contrary, ifcte2
doesn't needcte1
and both are needed only incte3
it should be considered to nest them under definition ofcte3
(with cte3 as (with cte1 as (...), cte2 as (...) select...)
).The syntax of CTEs then reflects the dependency tree between CTEs and literally visualizes the scope of partial datasets which can improve readability and prevents scope leakage bugs. Not all db vendors support it but Postgres does.
Example:
问题原因:在这里,您不必使用多个WITH子句来组合多个CTE。
解决方案:可以在SQL 中使用单个WITH 子句创建多个公共表表达式。两个不同的 CTE 使用单个 WITH 子句创建,并用逗号分隔以创建多个 CTE。
使用单个 With 子句的多个 CTE 示例
这是使用单个 With 子句创建多个公共表表达式的示例语法。
Problem Reason: Here, you don't have to use multiple WITH clause for combine Multiple CTE.
Solution: It is possible to create the Multiple Common Table Expression's using single WITH clause in SQL. The two different CTE's are created using Single WITH Clause and this is separated by comma to create multiple CTE's.
Sample Multiple CTE's using single
This is sample syntax for creating multiple Common Table Expression's with a single With Clause.