gpt4 book ai didi

sql - 列 '*' 在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中

转载 作者:行者123 更新时间:2023-12-03 02:30:38 24 4
gpt4 key购买 nike

我有一个如下查询

select a.EmployeeName, 

STUFF(( SELECT ',' + camTable.DepartmentName AS [text()]
FROM EmpoyeeDepartment subTable
left join DepartmentTable camTable on subTable.DepartmentID = camTable.DepartmentID
WHERE
subTable.EmployeeID = a.EmployeeID
FOR XML PATH('')
), 1, 1, '' )
AS Departments
from
EmployeeTable a
where a.EmployeeID = 144025
group by EmployeeName, Departments

但是当我执行上面的sql时,出现错误:

Column 'EmployeeTable.EmployeeID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

预期结果:

enter image description here

上面的sql有什么问题?

最佳答案

我认为这是因为 group by 子句中缺少 EmployeeID 列。子查询中指定的列也应包含在 group by 子句中。

请尝试:

select a.EmployeeName, 

STUFF(( SELECT ',' + camTable.DepartmentName AS [text()]
FROM EmpoyeeDepartment subTable
left join DepartmentTable camTable on subTable.DepartmentID = camTable.DepartmentID
WHERE
subTable.EmployeeID = a.EmployeeID
FOR XML PATH('')
), 1, 1, '' )
AS Departments
from
EmployeeTable a
where a.EmployeeID = 144025
group by EmployeeID, EmployeeName, Departments

关于sql - 列 '*' 在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22827948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com