日期:2014-05-20 浏览次数:20625 次
select * from test.score where (grade,name) in (select max(grade) as grade,name from test.score group by name)
------解决方案--------------------
不好意思,上面的语句是在自己的机子上测试的。改一下
select * from user where (grade,name) in (select max(grade) as grade,name from user group by name)
------解决方案--------------------
如果是用java代码的话,假设已经有user 实体,可以使用如下代码:
Comparator<User> comparator = new Comparator<User>(){ public int compare(User u1,User u2){ return u2.getGrade() - u1.getGrade();//排倒序 } }; Map<String,List<User>> users = new HashMap<String,List<User>>(); //key 为用户名,list 是每个相同用户名的n个实体 int index = 1; int length = users.values().iterator().next().size(); for(;index<=length;index++){ System.out.println("-------------第"+index+"高分----------"); for(String key;users.keySet()){ User user = users.get(key).get(index); System.out.println(user.getId()+"\t"+user.getName()+"\t"+user.getGrade()); } }
------解决方案--------------------
挺有意思的,需求明白了,你的要求是什么?用纯sql 还是 纯java?还是什么
------解决方案--------------------
order by grade desc
------解决方案--------------------
2个排序不就可以了
------解决方案--------------------
select *,(
select count(*) from Test001
where Test001.name=x.name and
Test001.id<>x.id and
Test001.grade>=x.grade) as row
from (select id,name,grade from Test001) as x
order by row;
------解决方案--------------------
Test001是表名,若是sqlserver2005/2008 或oracle 9之后都有 rownumber 函数就好办多了,
要是想从低到高可是把 Test001.grade>=x.grade 变为 Test001.grade《=x.grade
------解决方案--------------------
Test001是表名,若是sqlserver2005/2008 或oracle 9之后都有 rownumber 函数就好办多了,
要是想从低到高可是把 Test001.grade>=x.grade 变为 Test001.grade《=x.grade