有这样一张表,里面有很多用户的操作数据,现在想对每个角色ID进行分组,并同时按时间来排序取第一条的某个字段
我写了一个语句,结果跑了1小时30分钟还没有跑完,所有用户的日志数据量在1000万条左右,用的是SQL 2008的库
update a set a.curcharge_qichu = (select top 1 beforeNum from dbSDC.dbo.gold_log20140601 b where a.gameserverid = b.gameserverid and
a.userid = b.userid and time >= '20140501' and time < '20140601' order by time) from shenji_db.dbo.stat_may a
请问大神有没有更好的写法,或者更高效的方法
效果就是想取一下的data123,fengliang两个用户0的那一条
gameserverid userid beforeNum
202 dada123 0
202 dada123 50
202 fengliang 0
202 fengliang 50
------解决思路----------------------
用row_number()
------解决思路----------------------
select gameserverid,userid,beforeNum from (
select gameserverid,userid,beforeNum ,row_number() over(partition by gameserverid,userid order by beforeNum asc) as rId
from tablename
) as a where rId=1