作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个如下查询
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.
预期结果:
上面的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/
我是一名优秀的程序员,十分优秀!