日期:2014-05-17  浏览次数:20693 次

一条SQL语句....
表a
yxid       pm       cnt
a           12       2
a           13       1  
b           4         2
b           5         1
c           1         1
f           2         1
d           1         1
d           2         1
e           3         1
g           2         2

pm是给他们的排名,cnt是他们的这个排名有多少个.例如,a获得12的排名两个,获得13排名一个.要求输出的结果是,排名越前的,排在前面,例如,a获得1的排名两个,2的排名两个;b获得1的排名两个,2的排名一个,则a   就排在b的前面.以上输出的结果应该为:
d,c,g,f,e,b,a

------解决方案--------------------
create table t1(yxid varchar2(1),pm int,cnt int)
insert into t1
select 'a ',12, 2 from dual
union all
select 'a ',13, 1 from dual
union all
select 'b ',4, 2 from dual
union all
select 'b ',5, 1 from dual
union all
select 'c ',1, 1 from dual
union all
select 'f ',2, 1 from dual
union all
select 'd ',1, 1 from dual
union all
select 'd ',2, 1 from dual
union all
select 'e ',3, 1 from dual
union all
select 'g ',2, 2 from dual;
//
select yxid
from(
select yxid,sum(cnt/power((select count(*) from t1),pm)) p from t1
group by yxid
)t
order by t.p desc
//
1 d
2 c
3 g
4 f
5 e
6 b
7 a