select *  from 表 A
where not exists
(select 1 from 表 where picid=A.picid and provide=1 indatetime>A.indatetime and ID<A.ID)

解决方案 »

  1.   

    --或者:
    select * from 表 A
    where 
    indatetime=(select max(indatetime) from 表 where picid=A.picid and provide=1 )
      

  2.   

    --不好意思,有个Bug改下:
    --测试环境declare @t table(ID int,picid varchar(10),provide bit,defl varchar(200),indatetime datetime)
    insert into @t select 40,'20014',1,'有现货(不再生产)','2005-11-3 11:24:00'
    union all select 41,'20014',0,'款式不受欢迎','2005-11-2 11:24:05'
    union all select 46,'19972',1,'现货提供(生产中)','2005-11-7 11:24:43'
    union all select 47,'19972',0,'停产,不再继续供货','2005-11-8 11:24:48'
    union all select 52,'19984',1,'现货提供(生产中)','2005-11-20 13:28:04'
    --查询一
    select *  from @t A
    where not exists
    (select 1 from @t where picid=A.picid and indatetime>A.indatetime)
    and
    provide=1
    --查询二
    select * from @t A
    where 
    indatetime=(select max(indatetime) from @t where picid=A.picid ) 
    and provide=1 --结果ID          picid   provide    defl                     indatetime   
    --------------------------------------------------------------------
    40 20014 1 有现货(不再生产) 2005-11-03 11:24:00.000
    52 19984 1 现货提供(生产中) 2005-11-20 13:28:04.000
    ID          picid   provide    defl                     indatetime   
    --------------------------------------------------------------------
    40 20014 1 有现货(不再生产) 2005-11-03 11:24:00.000
    52 19984 1 现货提供(生产中) 2005-11-20 13:28:04.000