聚合函数或GROUP BY子句 [英] Either an aggregate function or the GROUP BY clause

查看:182
本文介绍了聚合函数或GROUP BY子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下查询:

select Patients.LastName, 
  avg (PatientVisits.Pulse)as pulse,
  avg (patientvisits.depressionlevel)as depressionLevel  
from Patients 
left join PatientVisits 
   on Patients.PatientKey=PatientVisits.PatientKey

但是出现以下错误:

消息8120(级别16,状态1,行1)在选择列表中的"Patients.LastName"列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中.

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

推荐答案

您需要在查询中添加GROUP BY:

select Patients.LastName, 
   avg (PatientVisits.Pulse)as pulse,
   avg (patientvisits.depressionlevel)as depressionLevel  
from Patients 
left join PatientVisits 
  on Patients.PatientKey=PatientVisits.PatientKey 
GROUP BY Patients.LastName

SQL Server要求SELECT列表中未包含在聚合函数中的所有列都包含在GROUP BY中.由于要在汇总数据时尝试返回Patients.LastName,因此必须将该列包括在分组依据中.

SQL Server requires any columns in your SELECT list that are not in an aggregate function be included in a GROUP BY. Since you are trying to return the Patients.LastName while you are aggregating the data you must include that column in a group by.

这篇关于聚合函数或GROUP BY子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆