当前位置: 代码迷 >> Sql Server >> 有哪位高手能帮小弟我看看这个sql语句如何写?待
  详细解决方案

有哪位高手能帮小弟我看看这个sql语句如何写?待

热度:74   发布时间:2016-04-27 14:01:53.0
有谁能帮我看看这个sql语句怎么写??在线等待
我现在有个数据表:
如:
ID UserID Score Addtime
1 0815001 60 2012-3-10
2 0815001 70 2012-3-11
3 0815001 80 2012-3-8
4 0815002 75 2012-3-8
5 0815002 70 2012-3-8
6 0815003 85 2012-3-6
7 0815003 90 2012-3-7
……

实现的效果是,选择出相同的UserID的最大分数的那些记录,预期的结果是:
3 0815001 80 2012-3-8
4 0815002 75 2012-3-8
7 0815003 90 2012-3-7


希望哪位高手能帮我看下这个sql语句是怎么写的?本人是新手,不太懂这些

------解决方案--------------------
SQL code
--> 测试数据:[t1]goif object_id('[t1]') is not null drop table [t1]gocreate table [t1]([ID] int,[UserID] varchar(7),[Score] int,[Addtime] datetime)goinsert [t1]select 1,'0815001',60,'2012-3-10' union allselect 2,'0815001',70,'2012-3-11' union allselect 3,'0815001',80,'2012-3-8' union allselect 4,'0815002',75,'2012-3-8' union allselect 5,'0815002',70,'2012-3-8' union allselect 6,'0815003',90,'2012-3-6' union allselect 7,'0815003',90,'2012-3-7'--假如存在相同分数,我取日期较大的那个;with tas(select ID,UserID,Score, Addtime from [t1] awhere Score=(select max(Score) from [t1] b where a.UserID=b.UserID))select * from t awhere Addtime=(select MAX(Addtime) from t b where a.UserID=b.UserID)--或者select * from(select ID,UserID,Score, Addtime from [t1] awhere Score=(select max(Score) from [t1] b where a.UserID=b.UserID))cwhere c.Addtime=(select MAX(Addtime) from (select ID,UserID,Score, Addtime from [t1] a where Score=(select max(Score) from [t1] b  where a.UserID=b.UserID))d where c.UserID=d.UserID) order by ID/*ID    UserID    Score    Addtime3    0815001    80    2012-03-08 00:00:00.0004    0815002    75    2012-03-08 00:00:00.0007    0815003    90    2012-03-07 00:00:00.000*/
  相关解决方案