给定以下合同表记录
Id EmployeeId StartDate EndDate
1 5601 2011-01-01 2011-09-01
2 5601 2011-09-02 2012-05-01
3 5601 2012-02-01 2012-08-01
4 5602 2011-01-01 2011-09-01
5 5602 2011-07-01 2012-10-01
每个员工可以有多个合同
我试图找到StartDate
比EndDate
每个员工更大的无效合同。
对于给定的结果Id=3
,Id=5
是无效的。我所做的是:
SELECT a.Id
FROM Contracts a
GROUP BY a.EmpId
HAVING a.StartDate > a.EndDate
但我得到这个错误:
Column 'Contract.Id' is invalid in the HAVING clause because it is not contained in either an aggregate function or the group by clause.
任何想法 ?