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

解决方案 »

  1.   

    select yxid from(
    select yxid,sum(pm*(10-Pm)) p from t1 group by yxid) t
    order by t.p desc
    10为表a的记录数
      

  2.   

    select *
    from(select yxid,sum(cnt/power(10,pm)) as c from t_a
    group by yxid
    )
    order by c desc
      

  3.   

    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
      

  4.   

    liuyxit(初学Oracle)
    按照你的方法,2的排位10个就和1的排位1个,就相等了,而真正的,是不相等的,1的排位1个是大于2的排位10个的
      

  5.   

    hongqi162(失踪的月亮)谢谢你!!!
      

  6.   

    问题是如果 a 获得 1个第一 2个第三 ,b获得3个第2 谁应该在前呢.  
    如果  a 获得 2个第一 2个第三 ,b获得4个第2 呢.  
    最好用算术加权平均
    select yxid,pm1/cnt1 as ord
    from 
    (select sum(pm) as pm1,sum(cnt) as cnt1 ,yxid from tab group by yxid) as tab1
    order by ord