SQL 消息 8120,级别 16

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21864733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 01:06:41  来源:igfitidea点击:

Msg 8120, Level 16

sqlsql-server

提问by user3325178

i want to join two tables with group by

我想通过 group by 加入两个表

select t.acno,t.name,count(Q.trans) as test,Q.transvalue,q.transdate 
from dbo.test t     
inner join dbo.testaqc q 
on t.acno=Q.acno 
group by q.trans

but i am receiving the error:

但我收到错误:

Msg 8120, Level 16, State 1, Line 1 Column 'dbo.test.acno' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

消息 8120,级别 16,状态 1,第 1 行列“dbo.test.acno”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中。

回答by M.Ali

select t.acno
      ,t.name
      ,count(Q.trans) as test
      ,Q.transvalue
      ,q.transdate 
from dbo.test t inner join dbo.testaqc q 
on t.acno = Q.acno 
group by t.acno,t.name,Q.transvalue,q.transdate 

Any column that is your select statement and not in an aggregate function must come in GROUP BY clause. as it says in the error message you are getting.

任何属于您的 select 语句且不在聚合函数中的列都必须出现在 GROUP BY 子句中。正如它在您收到的错误消息中所说的那样。