日期:2014-05-17  浏览次数:20569 次

SQL2005多行记录取最后日期的结果

如下数据:
 ID           BH        DJ           RQ
1001      99       10.1     2012-05-01
1001      99       10.2     2012-05-22
1001      99       20.1     2012-06-01
1001      99       11.1     2012-08-01
1002      99        7.2      2012-08-01
1002       99       7.8       2012-09-01
1001      90       11.1     2012-08-01
1002      90        7.2      2012-08-01
1002       90      7.8       2012-09-01
需显示结果如下:
1001      99       11.1     2012-08-01
1002      99       7.8      2012-09-01
1001      90       11.1     2012-08-01
1002      90       7.8      2012-09-01
------解决方案--------------------
select * from tb a
 where not exists(select 1 from tb where ID=a.ID and BH=a.BH and RQ>a.RQ)
------解决方案--------------------
select 
  *
from
  tb t
where
  rq=(select max(rq) from tb where id=t.id and bh=t.bh)