这似乎很简单,但对于我的生活无法弄清楚这一点。我有一个表EERate,有3列和类似的数据。
EmployeeNo RateAmt RaiseDate
C100 | 10.00 | 1/1/2013
C100 | 11.00 | 6/1/2013
C100 | 10.50 | 8/1/2013
C200 | 15.00 | 6/1/2013
C200 | 16.00 | 8/1/2013
我需要一个select语句,它将返回每个员工的最新数据。
Select EmployeeNo, Max(RaiseDate), RateAmt from EErates group by employeeNo
因为rateamt不是聚合函数而失败。
Select EmployeeNo, Max(RaiseDate), max(RateAmt) from EErates group by employeeNo
检索错误的数据。
有什么想法吗?
答案 0 :(得分:0)
SELECT EmployeeNo, RateAmt, RaiseDate FROM EErates o
WHERE RateAmt = (SELECT TOP 1 RateAmt FROM EErates i1 WHERE i1.EmployeeNo = o.EmployeeNo ORDER BY RaiseDate DESC)
AND RaiseDate = (SELECT TOP 1 RaiseDate FROM EErates i2 WHERE i2.EmployeeNo = o.EmployeeNo ORDER BY RaiseDate DESC)