2015-06-22 131 views
0
SELECT imps.Interest_name as PopularInterests, 
     imps.SUM(Count) AS Total_Count, 
     ints.Vertical_Name 
FROM Impressions imps 
    INNER JOIN Interests ints ON imps.Interest_name = ints.Interest_name 
GROUP BY imps.Interest_name 
HAVING imps.SUM(Count) > 10000; 

查询给我的错误:问题与功能

Lookup Error - SQL Server Database Error: Cannot find either column "imps" or the user-defined function or aggregate "imps.SUM", or the name is ambiguous

我的表如下所示:

印象:

Interest_Name Count 
Geo_934293  1107 
Geo_934293  852 
Geo_934293  934 
Geo_8133957  1810 
Geo_8133957  909 
Geo_8133957  463 
Geo_8133957  736 
Geo_8133957  7000 

垂直行业:

Interest_Name Vertical_Name 
Geo_934293   X 
Geo_8133957  Y 

和预期的结果将是:

Popular_Interest Total_Count Vertical_Name 
    Geo_8133957  10918    Y 

不知道我要去的地方错了吗?我认为它与SUM函数有关。

+0

_imps.SUM(计数)> 10000_这是什么? –

+1

SUM(Imps.count) – mohan111

回答

2

您需要使用SUM(imps.Count)而不是imps.SUM(Count)(它是无效的语法):

SELECT 
    imps.Interest_name as PopularInterests, 
    SUM(imps.Count) AS Total_Count, 
    ints.Vertical_Name 
FROM Impressions imps 
    INNER JOIN Interests ints ON imps.Interest_name = ints.Interest_name 
GROUP BY imps.Interest_name, ints.Vertical_Name 
HAVING SUM(imps.Count) > 10000; 
+0

感谢您使用Syntax帮助。在clasue组中,我可能还需要添加ints.Vertical_Name。没有它我得到一个错误。 –

+1

@TauseefHussain你是绝对正确的,在“GROUP BY”中,你需要添加聚合函数中未使用的所有列。我根据你的意见更新了答案 – dotnetom

+0

完美!非常感谢! –