代码出错... Staff.StaffId / Name总是被指出为错误。 想要在主管ID = 7和服务年限的条件下选择职员ID,风险和服务年限。 < 10
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
答案 0 :(得分:0)
最简单的解决方法是简单地向该函数添加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
答案 1 :(得分:0)
如果您有group by
并且想要多行,则需要count()
子句:
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 staffId, Name
having COUNT(*) < 10;
此外,不应对列名使用单引号。单引号只能用于字符串和日期常量。此外,最好使用表别名来标识每列的来源。
答案 2 :(得分:0)
我不确定你应该使用HAVING
- “AND
”会做......
select staffId,Name, DATEDIFF(year,DateJoin,GETDATE()) as 'Years in Service' from Staff
JOIN Branch ON Branch.BranchNo=Staff.BranchNo
where SupervisorID=7
and DATEDIFF(year,DateJoin,GETDATE()) <10