当前位置: 代码迷 >> Sql Server >> 请问SQL语句,来大神
  详细解决方案

请问SQL语句,来大神

热度:93   发布时间:2016-04-27 11:09:18.0
请教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 @Tselect 1,2,'2012-09-07 14:56:23',2,0 union allselect 1,2,'2012-09-07 16:44:14',2,0 union allselect 1,3,'2012-09-08 15:56:23',2,0select * from @T twhere 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           01           3           2012-09-08 15:56:23.000 2           0*/
  相关解决方案