我有一表﹕
date           code          value
2006/07/01     abc           123
2006/07/02     bcd           12
2006/07/04     abc           56
2006/07/06     abc           78
2006/07/09     bcd           465
.               .             .
.               .             .
.               .             .
現在我要取得每個code最大的那個date的那條記錄﹐也就是要得到以下結果
date           code          value
2006/07/06     abc           78
2006/07/09     bcd           456要求不用游標﹗

解决方案 »

  1.   

    SELECT b.mDate date,b.code code,a.value
    FROM a
    INNER JOIN 
    (SELECT MAX(date) mDate,code FROM tb GROUP BY code) b
    ON a.code=b.code AND a.date=b.mDate如果同code的最大date值有相同的,就另当别论,楼主需要给出条件,如何再选取.
      

  2.   

    Select * From TableName A 
    Where Not Exists (Select *From TableName Where code=A.code And [date]>A.[date])
      

  3.   

    SELECT b.mDate date,b.code code,a.value
    FROM tb a
    INNER JOIN 
    (SELECT MAX(date) mDate,code FROM tb GROUP BY code) b
    ON a.code=b.code AND a.date=b.mDate上面手误.假设 你的表名为 tb
      

  4.   

    select * from table a where not exists(select 3 from table b where code=a.code and date>a.date)
      

  5.   


    Select * From TableName A 
    Where [date]=(Select Max([date]) From TableName Where code=A.code)
      

  6.   

    fcuandy(要学的东西还很多)和我的三種方法都可以,不過fcuandy(要学的东西还很多)的更優。本來准備三種方法都寫的,被fcuandy(要学的东西还很多)搶了先了。:)
      

  7.   

    那我再加一种.
    SELECT * FROM tb a
    WHERE 1>(SELECT COUNT(1) FROM tb b WHERE b.code=a.code AND b.date>a.date)
    呵呵.
      

  8.   

    select * from @t a where (select count(*) from @t where code = a.code and date > a.date) = 0
    ^^