select * 
from tablename a
where not exists(select 1 from tablename where 编号=a.编号 and 时间>a.时间)

解决方案 »

  1.   

    select distinct * 
    from tablename
    where 编号='11'
    union all
    select * 
    from tablename a
    where not exists(select 1 from tablename where 编号=a.编号 and 类别='11') 
    and not exists(select 1 from tablename where 编号=a.编号 and 时间>a.时间)
      

  2.   


    --生成测试环境
    create table t1 (编号 varchar(3), 类别 varchar(2),时间 datetime,primary key(编号, 类别))
    insert into  t1
    select  '001','01','2005-8-1 08:20:01' union all
    select  '001','02','2005-8-1 08:21:01' union all
    select  '001','11','2005-8-2 08:23:01' union all
    select  '002','01','2005-8-1 09:22:01' union all
    select  '002','02','2005-8-2 09:23:01' union all
    select  '002','03','2005-8-2 09:24:04' union all
    select  '003','01','2005-8-1 10:20:01' union all
    select  '003','11','2005-8-2 10:23:01' union all
    select  '004','01','2005-8-1 10:25:01' union all
    select  '005','01','2005-8-1 10:25:01' 
    go
    --测试
    select 编号 ,类别,时间
    from t1 a where not exists
    ( select 1 from t1 where a.编号=编号 and 
     case 类别 when '11' then '9' else cast(时间 as varchar(25)) end >
     case a.类别 when '11' then '9' else cast(a.时间 as varchar(25)) end )
    --结果
    /*
    编号   类别   时间                                                     
    ---- ---- ------------------------------------------------------ 
    001  11   2005-08-02 08:23:01.000
    002  03   2005-08-02 09:24:04.000
    003  11   2005-08-02 10:23:01.000
    004  01   2005-08-01 10:25:01.000
    005  01   2005-08-01 10:25:01.000(所影响的行数为 5 行)*/
    --删除测试数据
    drop table t1
      

  3.   

    select * from 表 t
    where not exists(select 1 from 表 where [编号]=t.[编号] and [时间]>t.[时间])
          or
          [类别]=11
      

  4.   

    declare @tb table
    (
      [编号] varchar(10),
      [类别] varchar(10),
      [时间] datetime
    )
    insert @tb
    select '001','01','2005-8-1 08:20:01' union
    select '001','02','2005-8-1 08:21:01' union
    select '001','11','2005-8-2 08:23:01' union
    select '002','01','2005-8-1 09:22:01' union
    select '002','02','2005-8-2 09:23:01' union
    select '002','03','2005-8-2 09:24:04' union
    select '003','01','2005-8-1 10:20:01' union
    select '003','11','2005-8-2 10:23:01' union
    select '004','01','2005-8-1 10:25:01' union
    select '005','01','2005-8-1 10:25:01' --结果
    select * from @tb t
    where not exists(select 1 from @tb where [编号]=t.[编号] and [时间]>t.[时间])
          or
          [类别]=11--结果
    /*编号         类别         时间               
    ---------- ---------- --------------------------
    001        11         2005-08-02 08:23:01.000
    002        03         2005-08-02 09:24:04.000
    003        11         2005-08-02 10:23:01.000
    004        01         2005-08-01 10:25:01.000
    005        01         2005-08-01 10:25:01.000(所影响的行数为 5 行)
    */
      

  5.   

    上边的优点问题,修改如下:
    declare @tb table
    (
      [编号] varchar(10),
      [类别] varchar(10),
      [时间] datetime
    )
    insert @tb
    select '001','01','2005-8-1 08:20:01' union
    select '001','02','2005-8-1 08:21:01' union
    select '001','11','2005-8-2 08:23:01' union
    select '002','01','2005-8-1 09:22:01' union
    select '002','02','2005-8-2 09:23:01' union
    select '002','03','2005-8-2 09:24:04' union
    select '003','01','2005-8-1 10:20:01' union
    select '003','11','2005-8-2 10:23:01' union
    select '004','01','2005-8-1 10:25:01' union
    select '005','01','2005-8-1 10:25:01' --结果
    select * from @tb t
    where [类别]=11
          or
          ( not exists(select 1 from @tb where [编号]=t.[编号] and [时间]>t.[时间])
            and
            not exists(select 1 from @tb where [编号]=t.[编号] and [类别]=11)
          )
    --结果
    /*
    编号         类别         时间              
    ---------- ---------- ------------------
    001        11         2005-08-02 08:23:01.000
    002        03         2005-08-02 09:24:04.000
    003        11         2005-08-02 10:23:01.000
    004        01         2005-08-01 10:25:01.000
    005        01         2005-08-01 10:25:01.000(所影响的行数为 5 行)
    */
      

  6.   

    select * from @tb t
    where [类别]=11
          or
          ( not exists(select 1 from @tb where [编号]=t.[编号] and [时间]>t.[时间])
            and
            not exists(select 1 from @tb where [编号]=t.[编号] and [类别]=11)
          )