SQL抢分题,(老规矩,顶者有分)
现在有这部分数据:
case_id lab_id modify_time
----------- ----------- ----------------------------------
5002 26 2007-02-12 15:15:35.440
5002 27 2007-02-12 15:29:21.330
5002 28 2007-02-12 15:50:56.667
5003 29 2007-02-12 15:53:26.350
想要的结果是按case_id栏位分组,找出组内modify_time最大的数据。
结果应该是
case_id lab_id modify_time
----------- ----------- ----------------------------------
5002 28 2007-02-12 15:50:56.667
5003 29 2007-02-12 15:53:26.350
该怎么写SQL?
谢谢指教
------解决方案-------------------- select A.*
from 表名 as A
inner join (select case_id,max(modify_time) AS modify_time from 表名 group by case_id) as B on A.case_id=B.case_id and A.modify_time =B.modify_time
------解决方案-------------------- create table #t(case_id int,lab_id int,modify_time datetime)
insert into #t
select 5002,26, '2007-02-12 15:15:35.440 ' union all
select 5002,27, '2007-02-12 15:29:21.330 ' union all
select 5002,28, '2007-02-12 15:50:56.667 ' union all
select 5003,29, '2007-02-12 15:53:26.350 '
--如下SQL语句
select *
from #t as a
where not exists (select * from #t where case_id=A.case_id and modify_time> A.modify_time)
drop table #t
------解决方案--------------------顶!~~