How to reuse a sub query in sql?

前端 未结 1 579
梦如初夏
梦如初夏 2020-12-18 20:10

I have query like the following

select columns
from (select columns1
      from result_set
      where condition_common and condition1) as subset1
      join         


        
相关标签:
1条回答
  • 2020-12-18 20:33

    Use a Common Table Expression (CTE) if you're using SQL Server 2005+:

    with cte as (
          select columns
          from result_set
          where condition_common
        )
    select columns
    from cte  as subset1
          join
          cte as subset2 
             on subset1.somekey = subset2.somekey
    where otherconditions
    
    0 讨论(0)
提交回复
热议问题