id       result      time
01211    合格 2006-03-01
01211    不合格 2006-01-12 
05115    不合格 2006-03-01
05115    合格 2006-03-03 这样一个表,怎么查询出时间最小的每个id的result!~~?????
比如这里查询结果应该是id       result      time
01211    不合格 2006-01-12 
05115    不合格 2006-03-01

解决方案 »

  1.   

    select distinct id,result,time 
    from  tb as a
    where time=(select min(time) from tb where tb.id=a.id)
      

  2.   

    declare @t table(id varchar(10),result varchar(10),[time] varchar(10))
    insert into @t select '01211'   , '合格' ,'2006-03-01'
    union all select '01211'    ,'不合格' ,'2006-01-12' 
    union all select '05115'    ,'不合格' ,'2006-03-01'
    union all select '05115'    ,'合格' ,'2006-03-03'select * from @t a where exists(select 1 from @t where id=a.id and [time]>a.[time])
      

  3.   

    select id,result,min(time) as time
    from 表
    where result='不合格'
    group by id
      

  4.   


    select id,result,min(time) as time
    from @t
    where result='不合格'
    group by id,result
      

  5.   

    晕,回答问题太马糊了,再改~
    select id,result,min(time) as time
    from 表
    where result='不合格'
    group by id,result