1

我不断收到一条错误消息,显示“列名无效”'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)、活动、a 排序

4

1 回答 1

6

您不能按 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
于 2013-06-28T19:50:09.863 回答