我的SQL非常有限,我一直遇到这个代码的问题,我认为应该很简单,但我根本就没有得到它。我正在使用MS Access 2010和SQL。我得到的错误是查询不包括表达式作为聚合函数的一部分。
以下是实际代码:
SELECT
tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade,
tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName,
IIf(IsNull(CourseLength),"",DateAdd("m",[CourseLength],[Date])) AS ExpiryDate
FROM
tblEmployees
LEFT JOIN
(tblCourses
RIGHT JOIN
tblTrainingHistory ON tblCourses.CourseID = tblTrainingHistory.CourseID)
ON tblEmployees.EmpID = tblTrainingHistory.EmpID
WHERE
(((tblCourses.CourseName) In ("Course1","Course2","Course3","Course4","Course5"))
AND ((IIf(IsNull([CourseLength]),Now(),DateAdd("m",[CourseLength],[Date])))>Now())
AND ((tblEmployees.Active)=True))
GROUP BY
tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade,
tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName, tblEmployees.EmpID
HAVING
(((Count(*))=4));
所以我想要完成的是一个查询,它将为我提供所有活跃在数据库中的员工,他们必须拥有所有5个课程。我可以培养每个课程的人,但我只需要一个简单的每个人的课程清单。
谢谢
答案 0 :(得分:0)
您还需要在CourseLength
上添加分组,如果我理解正确,您需要COUNT(*) = 5
:
SELECT
tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade,
tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName,
IIf(IsNull(CourseLength),"",DateAdd("m",[CourseLength],[Date])) AS ExpiryDate
FROM
tblEmployees
LEFT JOIN
(tblCourses
RIGHT JOIN
tblTrainingHistory ON tblCourses.CourseID = tblTrainingHistory.CourseID)
ON tblEmployees.EmpID = tblTrainingHistory.EmpID
WHERE
(((tblCourses.CourseName) In ("Course1","Course2","Course3","Course4","Course5"))
AND ((IIf(IsNull([CourseLength]),Now(),DateAdd("m",[CourseLength],[Date])))>Now())
AND ((tblEmployees.Active)=True))
GROUP BY
tblEmployees.LName, tblEmployees.FName, tblEmployees.Trade,
tblEmployees.Title, tblTrainingHistory.Date, tblCourses.CourseName, tblEmployees.EmpID,
tblCourses.CourseLength
HAVING
(COUNT(*) = 5);
另一种选择是将Aggregate function
MAX
或MIN
中的任何“未组合”列包装。