如何在sql中重用一个子查询?

我有如下的查询
select columns
from (select columns1
      from result_set
      where condition_common and condition1) as subset1
      join
      (select columns2
       from result_set
       where condition_common and condition2) as subset2 
      on subset1.somekey = subset2.somekey

我想以某种方式重用

select columns
from result_set
where condition_common

我以上的查询过于简单,但上述的选择在现实中是巨大而复杂的.我不想有确保两者都同步的负担

我没有任何方法以编程方式重用它. T-sql被排除.我只能写简单的查询.这是一个应用限制.

有没有办法在单个语句中重用同一个查询

解决方法

如果您使用sql Server 2005,请使用 Common Table Expression(CTE):
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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...