Sql错误:在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中

时间:2015-08-12 19:00:23

标签: sql

我有一个查询,我试图在sql中执行给我一个错误:

SELECT this_.pName as y0_
       , this_.kNum as y1_
       , aid1_.AID as y2_
       , COUNT(DISTINCT this_.agentG) as y3_ 
FROM AM_CPView this_
INNER JOIN AM_MView aid1_ ON this_.agentG= aid1_.AgentG
GROUP BY this_.pName, this_.kNum

这会出错:

Column 'AM_MView.AID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. 

任何人都可以帮我解决这个错误吗?感谢

1 个答案:

答案 0 :(得分:1)

当您在SELECT中使用聚合函数(例如计数)并且您还按非聚合列进行分组时,SELECT中的所有非聚合列也必须出现在GROUP BY中{1}}。因此,您的查询应该通过以下方式修复:

SELECT this_.pName as y0_
       , this_.kNum as y1_
       , aid1_.AID as y2_
       , COUNT(DISTINCT this_.agentG) as y3_ 
FROM AM_CPView this_
INNER JOIN AM_MView aid1_ ON this_.agentG= aid1_.AgentG
GROUP BY this_.pName, this_.kNum, aid1_.AID