sql - 嵌套 CTE 的 SQL
问题描述
现在我的 SQL 代码如下:
WITH cte1 as(....),
cte2 as(....),
cte3 as (...)
select *
from cte1
left join cte2
on cte1.col1=cte2.col2
left join cte3
on cte1.col2=cte3.col3
现在我想使用上面的 SQL 代码根据结果进行进一步的查询(例如过滤器)。我尝试使用嵌套的 WITH 语句,但它不支持。
WITH a as (
WITH cte1 as(....),
cte2 as(....),
cte3 as (...)
select *
from cte1
left join cte2
on cte1.col1=cte2.col2
left join cte3
on cte1.col2=cte3.col3
)
是否有任何有效的方法可以根据上述 SQL 代码的结果进行进一步查询?
解决方案
埃里克,你不嵌套它们,只是序列:
WITH cte1 as(....),
cte2 as(....),
cte3 as (...),
another_cte as
(
select *
from cte1
left join cte2
on cte1.col1=cte2.col2
left join cte3
on cte1.col2=cte3.col3
)
select <whatever> from another_cte
至于efficient way to do further query
您可能对“非 SQL”感兴趣,并将原始查询的结果保存在临时表或变量中。根据您使用的数据库,执行计划可能会更快/更慢,因此请检查一下。
推荐阅读
- bash - 通过命令行传递一个数组以使用 getopts() 处理
- python - 用 python / rdflib 解析海龟,不能指定 IRI 前缀
- html - 设置图像的默认大小,如果超过或太小则进行裁剪/放大
- java - 如何通过带有 HATEOAS 的 @JoinTable 获取信息
- node.js - MQTT 订阅者是否需要静态 IP?
- c# - 为什么我的 WebAPI 服务调用无法通过 IIS 找到文件夹路径的一部分?
- c++ - 具有范围的随机数生成器?C++
- python - 理解 TensorFlow 中的 ResourceVariables
- python - 如何对我的软件进行编程以将其数据保存到特定服务器?
- docker - Docker Alpine dotnet Core 2.2 Console App 每个响应内容长度 0