我有一个查询,我试图在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.
任何人都可以帮我解决这个错误吗?感谢
答案 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