繁体   English   中英

SQL:两个表的多个CTE的并集

[英]SQL: Union of two tables multiple CTEs

我的目标是在SQL中with table as alias两个表,其中每个表都很复杂,并且必须使用with table as alias语法定义自己的公用表表达式。

因此,我们有了具有以下语法的第一个表:

with table1 as (
select/from/where statement),

table2 as (
select/from where statement),

select table1 join table2 on [...]

第二张表,具有类似的语法:

with table3 as (
select/from/where statement),

table4 as (
select/from where statement),

select table3 join table4 on [...]

我的问题是:简单地把union这两个表之间不剪 编译器将上面两个表之间的union视为错误的表达式。

(我的另一种方法是将整个第一个表定义为table5,将整个第二个表定义为table6,之后再将它们合并,这是因为您似乎无法将多个“ with as”语句相互堆叠。)

您必须将所有CTE放在第一个SELECT之前,但是然后可以像对待表一样对其进行UNION:

WITH Table1 AS (..)
, Table2 AS (..)
, Table3 AS (..)
, Table4 AS (..)
SELECT Table1 JOIN Table2..
UNION 
SELECT Table3 JOIN Table4..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM