Sqlserver
 sql >> Database >  >> RDS >> Sqlserver

Come riutilizzare una query secondaria in sql?

Utilizzare un Espressione di tabella comune (CTE) se utilizzi 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