当前位置: 代码迷 >> Sql Server >> 查询每个人最晚的纪要!求搭救
  详细解决方案

查询每个人最晚的纪要!求搭救

热度:71   发布时间:2016-04-24 20:33:08.0
查询每个人最晚的纪录!!求搭救

index userid workid       time 
 1      1      1     2013-07-23 12:38:10
 2      2      2     2013-07-23 12:38:11
 3      3      3     2013-07-23 12:38:12
 4      1      4     2013-07-23 12:38:13
 5      2      5     2013-07-23 12:38:14
 6      3      6     2013-07-23 12:38:15
 
想要查出每个人最晚那条记录 如
 4      1      4     2013-07-23 12:38:13
 5      2      5     2013-07-23 12:38:14
 6      3      6     2013-07-23 12:38:15

求搭救
sql?查询?

------解决方案--------------------
select * from 表 a
where time=(select max(time) from 表 where a.userid=userid)
------解决方案--------------------
select a.*
from tb a
inner join (select userid,max(time) as time from tb group by userid)b
on a.userid=b.userid and a.time=b.time
------解决方案--------------------
create table tb([index] int, userid int,workid int,[time] datetime) 
insert into tb
select 1,1,1,'2013-07-23 12:38:10'
union all select 2,2,2,'2013-07-23 12:38:11'
union all select 3,3,3,'2013-07-23 12:38:12'
union all select 4,1,4,'2013-07-23 12:38:13'
union all select 5,2,5,'2013-07-23 12:38:14'
  相关解决方案