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
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
from tb as a
where time=(select min(time) from tb where tb.id=a.id)
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])
from 表
where result='不合格'
group by id
select id,result,min(time) as time
from @t
where result='不合格'
group by id,result
select id,result,min(time) as time
from 表
where result='不合格'
group by id,result