本文介绍了聚合函数或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
:
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子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!