Sql server SQL-是否在子查询的结果中再添加一列?SQL Server 2008

Sql server SQL-是否在子查询的结果中再添加一列?SQL Server 2008,sql-server,sql-server-2008,tsql,join,Sql Server,Sql Server 2008,Tsql,Join,我有一个查询结果非常好: select A.ID_acc, A.ID_us, A.st, table3.KFL, '100' as myattribute, '101' as my attribute2 from SOURCE1 as A left join (select table2.ID_us, table2.ID_acc, CASE WHEN table2.KFL_type = 'KFL' THEN

我有一个查询结果非常好:

select 
    A.ID_acc, A.ID_us, A.st, table3.KFL, 
    '100' as myattribute, 
    '101' as my attribute2
from 
    SOURCE1 as A
left join
    (select 
         table2.ID_us, table2.ID_acc, 
         CASE WHEN table2.KFL_type = 'KFL' THEN P.index_num ELSE table2.KFL_type END as KFL
     from
         (select 
              table1.ID_us, table1.ID_acc, 
              CASE WHEN sum(table1.count_kfl) > 1  THEN '9999' WHEN sum(table1.count_kfl) = 1 THEN 'KFL' END as KFL_type
         from
             (SELECT 
                  ID_us, ID_acc, count(*) as count_kfl 
              FROM 
                  payments
              WHERE
                  index_num IN (200, 201, 203) 
                  AND (date >= XXXX or date2 >= 'XXXXX') 
              GROUP BY
                  1, 2) as table1
         group by 
            1, 2) as table2 
     join 
         SOURCE2 as P on table2.ID_us = P.ID_us 
                      and table2.ID_acc = P.ID_acc
     where 
         (P.date>= XXXX or P.date2 >= 'XXXXX') 
         and index_num in (201,201,203)
     group by 
         1, 2
     order by 
         1, 2) as table3 on table3.ID_us = A.ID_us 
                         and table3.ID_acc = A.ID_acc 
where 
    A.not_deleted >= XXXXXX
这个查询不是我的主要问题,所以我只是将其复制为简短的摘要,但我想知道现在如何添加一个额外的列(计数操作的结果)作为第一个查询的结束?只是为了不使2个单独,然后混合结果。当然,我不想影响我以前的成绩

我有第二个查询,如下所示:

select A.ID_us, count(*)/2 as number
from
 SOURCE1 as A
left join
SOURCE3 as B
on A.ID_acc = B.ID_acc
where A.date >= XXXX
group by 1
这两个查询之间的链接是源A中的属性ID_acc,它出现在第一个查询和第二个查询中

但我不知道怎么做

select A.ID_acc, A.ID_us, A.st, table3.KFL, '100' as myattribute, '101' as my attribute2, NEWSOURCE.MYNEW_attribute
from SOURCE1 as A
left join
(
...
)
as table3 on table3.ID_us = A.ID_us and table3.ID_acc = A.ID_acc 
where A.not_deleted >= XXXXXX
left join
(
.
.
.
)
as NEWSOURCE

当然,类似这样的内容不起作用://

您是否尝试过相关子查询:

select A.ID_acc, A.ID_us, A.st, table3.KFL, '100' as myattribute, 
'101' as my attribute2,
( select count(*)/2 as number from SOURCE1 as IA left join SOURCE3 as IB on
  IA.ID_acc = IB.ID_acc and IA.ID_Acc = A.ID_Acc where IA.date >= XXXX ) as NewColumn
 from ...
请注意在相关子查询中使用的新别名