我有如下数据
ID Stat Date 1 1 2009-06-01 2 1 2009-06-20 3 1 2009-06-10 4 2 2009-06-10
O / P就是这样。
ID Stat CDate 2 1 2009-06-20 4 2 2009-06-10
我已尝试使用以下查询,但未成功,请提出建议。
Select Distinct stat,MAX(Cdate) dt,id From testtable Group By stat,id
得到了解决方案..
从测试表中选择f1.id,f1.stat,f1.cdate作为F1加入(从stat从测试表组中选择stat,MAX(cdate)作为dt)作为F2在f2.stat= F1.stat和f2.dt = f1.cdate
SELECT t1.id, t1.stat, t1.date FROM testtable t1 JOIN (SELECT stat, MAX(date) date FROM testtable GROUP BY stat) t2 ON t1.stat = t2.stat AND t1.date = t2.date GROUP BY stat