繁体   English   中英

无效的列名称SQL Server 2008

[英]Invalid Column Name SQL Server 2008

我不断收到错误消息,指出“无效列名”为“ a”。 有人可以帮忙吗?

Select 
datename(mm,t.gift_date) as month, 
s.campaign as campaign, 
count(s.Campaign) as a
From usga_stage.facts.transactions t, usga_dw.dbo.DimSourceCode s
Where t.trans_source=s.SourceCode
and t.payment_channel = 'Web'
and t.gift_date like '2013%'
Group by month(t.gift_date),Campaign, a

按月份排序(t.gift_date),广告系列,

您不能按ALIAS分组,也不需要按汇总字段分组:

Select 
month(t.gift_date) as month, 
s.campaign as campaign, 
count(s.Campaign) as a
From usga_stage.facts.transactions t, usga_dw.dbo.DimSourceCode s
Where t.trans_source=s.SourceCode
and t.payment_channel = 'Web'
and t.gift_date like '2013%'
Group by month(t.gift_date),Campaign

您可以在ORDER BY部分中使用别名,而不是GROUP BY

同样,使用显式联接也是一个好习惯,如下所示:

SELECT month(t.gift_date) as month
      ,s.campaign as campaign
      ,count(s.Campaign) as a
FROM usga_stage.facts.transactions t
JOIN usga_dw.dbo.DimSourceCode s
  ON t.trans_source = s.SourceCode
WHERE t.payment_channel = 'Web'
  and t.gift_date like '2013%'
GROUP BY MONTH(t.gift_date)
        ,Campaign

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM