日期:2014-05-18  浏览次数:20405 次

SQL语句问题,详情请进
LeiXing     value     xingming
1001             90           学生1
1001             30           学生2
1001             80           学生3
1001             52           学生4
1002             50           学生5
1002             62           学生6
1002             60           学生7
1002             30           学生8
1002             50           学生9

用语句得到LeiXing   相同的前三位的xingming   和value  
1001             90           学生1
1001             80           学生3
1001             52           学生4
1002             62           学生6
1002             60           学生7
1002             50           学生5
1002             50           学生9
 


------解决方案--------------------
create table test(LeiXing int,value int,xingming varchar(20))
insert test select 1001,90, '学生1 '
union all select 1001,30, '学生2 '
union all select 1001,80, '学生3 '
union all select 1001,52, '学生4 '
union all select 1002,50, '学生5 '
union all select 1002,62, '学生6 '
union all select 1002,60, '学生7 '
union all select 1002,30, '学生8 '
union all select 1002,50, '学生9 '


select * from test a where value in
(
select top 3 value from test where LeiXing=a.LeiXing order by value desc
)