日期:2014-05-17  浏览次数:20469 次

请教SQL语句,来大神
userid reguid xftime xfpoint sypoint
1 2 2012-09-07 14:56:23 2 0
1 2 2012-09-07 16:44:14 2 0
1 3 2012-09-08 15:56:23 2 0


这个是表结构

我想达到的目的是通过userid 查询 reguid 的最新一条记录 最后的效果如下
1 2 2012-09-07 16:44:14 2 0
1 3 2012-09-08 15:56:23 2 0

SQL语句怎么写?

------解决方案--------------------
SQL code

declare @T table (userid int,reguid int,xftime datetime,xfpoint int,sypoint int)
insert into @T
select 1,2,'2012-09-07 14:56:23',2,0 union all
select 1,2,'2012-09-07 16:44:14',2,0 union all
select 1,3,'2012-09-08 15:56:23',2,0

select * from @T t
where xftime=(select max(xftime) from @T where reguid=t.reguid)
and userid=1
/*
userid      reguid      xftime                  xfpoint     sypoint
----------- ----------- ----------------------- ----------- -----------
1           2           2012-09-07 16:44:14.000 2           0
1           3           2012-09-08 15:56:23.000 2           0
*/