我有两个sql查询,如下所示
1. ;with test as (.........)
select abc from (select ... from test)
2. ;with test1 as (....)
select abc from (select ... from test1)
两个查询都具有相同的列,但条件不同。我想将这些结果合并到一个数据文件中。
我尝试过查询1联合查询2,但是
当遇到';'时,我无法使用union作为“''附近的语法错误;'”第二个查询
答案 0 :(得分:3)
您可以在一个查询中声明多个CTE:
WITH Test AS (
SELECT ...
),
Test1 AS (
SELECT ..
)
SELECT <column_list> FROM Test
UNION
SELECT <column_list> FROM Test1;
答案 1 :(得分:1)
使用堆叠CTE
;with test as (.......),
test1 as (......)
select abc from (select ... from test)
union all
select abc from (select ... from test1)
显示原始查询,可能是建议的更好的替代方案
答案 2 :(得分:0)
这样的事情......?你的SQL很不清楚,因为它已被大量编辑。
multiDexEnabled true
答案 3 :(得分:0)
这个问题有两种方法:
方法1:
With test as (..........),
test1 as (.........)
Select * from test
UNION
Select * from test1;
方法2:
Create view test as (.....)
Create view test1 as (.....)
Select * from test
UNION
Select * from test1;