SQL Server 2012,排除列

SQL Server 2012, exclude column

我有SQL查询

; with cte as 
(
   SELECT 
       PARSENAME(REPLACE(replace(replace(replace(replace(dbo.IDENTITY_MAP.Name, 'My Company\', ''), '-VLAN2', ''), '.VLAN2\', ''), '.Instr\', '') , '\' , '.'), 1) as "Site",
       Count (CASE
                 WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%' THEN 1
              END) AS 'SEP-11',
       Count (CASE
                 WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '12.%' THEN 1
              END) AS 'SEP-12',
   FROM   
       dbo.sem_computer
   INNER JOIN 
       [dbo].[V_SEM_COMPUTER] ON [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID
   WHERE 
       dbo.IDENTITY_MAP.Name NOT LIKE '%Servers%'
   GROUP BY 
       PARSENAME(REPLACE(replace(replace(replace(replace(dbo.IDENTITY_MAP.Name,'My Company\',''),'-VLAN2',''),'.VLAN2\',''),'.Instr\','') , '\' , '.'),1)
)
select *
from cte
join SEPM_site ss on cte.Site = ss.Site

这给出了我正在寻找的输出 ------ 几乎即

Site  SEP-11  SEP-12 Rackcode  Circuit  Site

我只需要一栏 Site

我尝试用这些列重新创建一个临时 table,然后删除它,即

; with cte as (SELECT ...)
select * into temptable
from cte
join SEPM_site ss
 on cte.Site = ss.Site
alter table temptable
drop column cte.Site
select * from temptable
drop table temptable

但我收到错误

Incorrect syntax near '.'

如果我不指定 table Site 来自哪个,我会收到错误消息,

Column names in each table must be unique. Column name 'Site' in table 'temptable' is specified more than once.

但这就是我尝试删除重复列的原因!

谢谢!

只需在 select 语句中指定您想要的列:

select cte.Site, cte.[SEP-11], cte.[SEP-12], ss.Rackcode, ss.Circuit
from cte
join SEPM_site ss
 on cte.Site = ss.Site

您还可以 select cte 中的所有列以及 ss 中您想要的列:

select cte.*, ss.Rackcode, ss.Circuit
from cte
join SEPM_site ss
 on cte.Site = ss.Site