How can I have multiple common table expressions in a single SELECT statement?

前端 未结 2 1314
[愿得一人]
[愿得一人] 2021-01-30 00:18

I am in the process of simplifying a complicated select statement, so thought I would use common table expressions.

Declaring a single cte works fine.

WI         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 00:39

    Above mentioned answer is right:

    WITH 
        cte1 as (SELECT * from cdr.Location),
        cte2 as (SELECT * from cdr.Location)
    select * from cte1 union select * from cte2
    

    Aditionally, You can also query from cte1 in cte2 :

    WITH 
        cte1 as (SELECT * from cdr.Location),
        cte2 as (SELECT * from cte1 where val1 = val2)
    
    select * from cte1 union select * from cte2
    

    val1,val2 are just asumptions for expressions..

    Hope this blog will also help : http://iamfixed.blogspot.de/2017/11/common-table-expression-in-sql-with.html

提交回复
热议问题