函数

时间:2015-06-22 08:51:41

标签: sql sql-server

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;

查询给出了错误:

  

查找错误 - SQL Server数据库错误:找不到任一列   “imps”或用户定义的函数或聚合“imps.SUM”,或者   名字含糊不清

我的表格如下:

曝光:

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函数有关。

1 个答案:

答案 0 :(得分: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;