扩展以下问题(多重选择语句),我想知道是否可以执行以下操作:
WITH cte1 as ( SELECT * from cdr.Location ), cte2 as ( SELECT * from cdr.Location WHERE cdr.Location.someField = cte1.SomeField ) select * from cte1 union select * from cte2
所以这里的重点是以下几行:
WHERE cdr.Location.someField = cte1.SomeField
我在cte2中的哪里引用cte1?
是的,您可以在后续CTE中引用先前声明的CTE:
WITH cte1 as ( SELECT t.* FROM cdr.Location t), cte2 as ( SELECT t.* FROM cdr.Location t JOIN cte1 c1 ON c1.somefield = t.someField) SELECT * FROM cte1 UNION SELECT * FROM cte2
笔记
顺便说一句:尝试在将来树立更好的榜样-这对您和所有试图帮助您的SO社区其他成员都是有益的。