分组取最新记录的SQL

常遇到这样的情况,要取得所有客户的最新交易记录,读取网站所有浏览者最后一次访问时间。一个客户只读取最新的一次记录,相同,大部分的人首先想 到的就是排除所有记录,相同的只取一条。用distint,但是distint只能取到一个字段的值。所以使用distint就不能读取 出所有的情况。下面是一条正确的语句:
select   *   from   表名    自定义  
  where   时间字段=(select   max(时间字段)   from   表名   where   客户ID=自定义.客户ID)
例如: select   *   from   GradeEmployee    a  
  where   endTime=(select   max(endTime)   from   GradeEmployee   where   employeeID=a.employeeID)

转:http://my.oschina.net/renyuansoft/blog/6921?from=rss

posted @ 2011-12-23 14:53  catgatp  阅读(547)  评论(0编辑  收藏  举报