我正在创建一个查询,不知怎的,我的查询中似乎出现了问题。
这是我的问题:
Select
E.last_name as [Last Name],
E.first_name as [First Name],
SUM(CASE WHEN empAttendance.status = 'Absent' THEN 1 ELSE 0 END) as [Absences],
SUM(CASE WHEN empAttendance.status = 'Late' THEN 1 ELSE 0 END) as [Number of Lates]
from
empAttendance
INNER JOIN employee E ON empAttendance.emp_id = E.emp_id
WHERE E.company_id = (Select company_id from company Where company_name = @company)
其中employee表的emp_id列为PK,empAttendance的emp_id为FK。
员工表包含last_name
和first_name
列。
错误说明:Column 'employee.last_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
提前致谢。
答案 0 :(得分:4)
使用汇总功能SUM
时,您需要group by
:
Select
E.last_name as [Last Name],
E.first_name as [First Name],
SUM(CASE WHEN empAttendance.status = 'Absent' THEN 1 ELSE 0 END) as [Absences],
SUM(CASE WHEN empAttendance.status = 'Late' THEN 1 ELSE 0 END) as [Number of Lates]
from
empAttendance
INNER JOIN employee E ON empAttendance.emp_id = E.emp_id
WHERE E.company_id = (Select company_id from company Where company_name = @company)
GROUP BY E.last_name, E.first_name