【问题标题】:column is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BYHAVING 子句中的列无效,因为它不包含在聚合函数或 GROUP BY 中
【发布时间】:2016-04-30 21:30:04
【问题描述】:

我试图使我的列等于过滤器使用有,但问题是它给出了这个错误消息:

column is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY

当我尝试在 select 和 group by 中添加过滤器时,它会说:

Column names in each view or function must be unique

因为列和过滤器都是一样的:

专栏:

     userTable.userid

过滤器:

  user_filter.userid

这是我的代码:

SELECT  DISTINCT 
userId,
username,
sum(userItem)
from userTable inner join user_filter
on userTable.userName =  user_filter.userName 
group by userId,
username,
having  userTable.userId =  user_filter.userId

【问题讨论】:

  • 编辑您的问题并包含生成错误的查询。
  • 好的,我会添加查询
  • 你真正想要达到什么目的?当两个表都有userid 时,你为什么要通过username 加入?当username 已经在usertable 中时,为什么userfilter 仍然有username
  • 您使用的是什么 DBMS? MySQL、SQL Server、PostgreSQL、...?

标签: sql filter


【解决方案1】:

您不需要Having 子句,只需在Where 子句中添加另一个过滤器

SELECT userid, 
       username, 
       Sum(useritem) 
FROM   usertable 
       INNER JOIN user_filter 
               ON usertable.username = user_filter.username 
                  AND usertable.userid = user_filter.userid 
GROUP  BY userid, 
          username 

另外DISTINCT 是多余的,因为Group by 已经在select 中使用了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 2014-11-02
    • 2011-07-04
    • 2013-08-17
    相关资源
    最近更新 更多