代码出错... Staff.StaffId/Name总是被指出为错误。
想要选择职员ID,anem和服务年限,条件是主管ID = 7并且“服务年限”

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


select staffId,Name, DATEDIFF(year,DateJoin,GETDATE()) as 'Years in Service' from Staff
JOIN Branch ON Branch.BranchNo=Staff.BranchNo
where SupervisorID=7
having COUNT(*) <10

最佳答案

最简单的解决方法是将Group By子句添加到函数中。尝试:

select staffId,Name, DATEDIFF(year,DateJoin,GETDATE()) as 'Years in Service' from Staff
JOIN Branch ON Branch.BranchNo=Staff.BranchNo
where SupervisorID=7
GROUP BY staff.name, staff.staffId
having COUNT(*) <10

关于asp.net - 由于group by子句,撤消无效错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24025565/

10-13 05:45