现有一张表
数据如下
  spid  date       price
  002  2007-04-09  10.0
  003  2007-04-03   50.0
  003  2007-04-13  12.0
  004  2007-04-04  12.0
  005  2007-04-13  140.0
  004  2007-04-15  40.0
  005  2007-04-17  240.0
想到的结果是:
  spid  date       price
   002  2007-04-09   10.0
   003  2007-04-13  12.0
   004  2007-04-15  40.0
   005  2007-04-17  240.0 也就是求最新插入数据库中的记录是那些
请问这个延长样解决

解决方案 »

  1.   

    select a.* from table a 
    where exists (select 0 from (select spid  max(date) as date  from tbale) b 
    where a.spid  = b.spid  and a.date  = b.date )
    --这样?
      

  2.   

    select a.* from a a ,(select spid , max(date) as date from a group by spid)b where a.spid=b.spid 
    and a.date=b.date order by  a.spid
      

  3.   


    select a.* from a a where a.date in (select max(b.date) as date from a b  where a.spid=b.spid )order by a.spid
      

  4.   

    SELECT A.SpId, A.Data, A.Price 
    FROM Table1 AS A 
    INNER JOIN 
    (
        SELECT SpId, MAX(Date) AS MaxDate 
        FROM Table1 
        GROUP BY SpId 
    ) AS B 
    ON A.SpId=B.SpId AND A.Date=B.MaxDate 
    ORDER BY A.SpId