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

前端 未结 2 1313
[愿得一人]
[愿得一人] 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:27

    I think it should be something like:

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

    Basically, WITH is just a clause here, and like the other clauses that take lists, "," is the appropriate delimiter.

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题