我有以下结构:
Create @temp
Select ...inser...into @temp where ...
(select ... from @temp
Join tblA where ... )
UNION
(Select ... from @temp
join tblB where ... )
在构建上面的表之后,我需要能够执行WHERE,JOINS,...
类似的东西:
Select ... from (above statement)
join ....
where....
我不知道@ temp,join,union ......是否可以在其他select中。
或者我唯一能做的就是用第一个语句结果创建一个@ Temp2插入,然后使用其他连接,其中......?
更新1:
我也在尝试:
With cte (query returned columns)
as
(same query I was using to build my @temp as before)
(select ... from cte
join tblA
where...)
UNION
(select ... from cte
join tblB
where...)
但我在如何执行其他联接方面仍处于同一点,其中......总结果高于
答案 0 :(得分:2)
Create @temp
Select ...inser...into @temp where ...
;with temp2 as
(
select ... from @temp Join tblA where ...
UNION
Select ... from @temp join tblB where ...
)
select ... from temp2
join ....
where....
答案 1 :(得分:1)
实际上你可以在没有临时表的情况下做到这一点:
WITH myCTE [ ( column_name [,...n] ) ]
AS
( here you define your query )
之后您只需选择但使用CTE
Select ... from myCTE
join ....
where....
关于CTE你可以阅读Here 更新后
Select fields from myCTE join table1
Union
Select fields from myCTE join table2
查询中没有括号