• Oracle中真正稳妥的求三甲的方法


    坐地铁回家路上忽然想起,三甲排名可能为多个,只取三名岂不荒谬。不信请看下面数据:

    create table tb_score(
        id number(4,0) primary key,
        name nvarchar2(20) not null,
        score integer not null)
    
    insert into tb_score values('1','Andy','100');
    insert into tb_score values('2','Bill','99');
    insert into tb_score values('3','Cindy','100');
    insert into tb_score values('4','Douglas','99');
    insert into tb_score values('5','Eliot','98');
    insert into tb_score values('6','Flex','98');
    insert into tb_score values('7','Hellen','98');
    insert into tb_score values('8','jack','97');
    insert into tb_score values('9','king','95');
    insert into tb_score values('10','tim','92');
    insert into tb_score values('11','yang','91');

    明显100分的状元有Andy,Cindy两人,99分的榜眼也有两人,98分的探花有三人。

    正确的做法是按分数分组,名字累加,再取分数最高的三个等级,SQL如下:

    select b.* from
    (
        select a.*,rownum as rn  from
        (
            select listagg(name,',') within group (order by name) as names from tb_score
            group by score
            order by score  desc
        ) a
    ) b
    where b.rn<4

    数据如下:

    SQL> select b.* from
      2  (
      3  select a.*,rownum as rn  from
      4  (
      5  select listagg(name,',') within group (order by name) as names from tb_score
      6  group by score
      7  order by score  desc
      8  ) a
      9  ) b
     10  where b.rn<4;
    
    NAMES                                    RN
    -------------------------------- ----------
     A n d y, C i n d y                       1
     B i l l, D o u g l a s                   2
     E l i o t, F l e x, H e l l e n          3

    这才是公平公正和稳妥的排名。

    --2020年4月23日--

  • 相关阅读:
    git track
    npm 升级到最新版本
    三行代码实现垂直居中和cube
    布局之定位
    MongoDB的安装问题
    正则表达式
    javascript表单验证
    Oracle中创建表,行级触发器,序列
    查找某个字符在字符串中出现的次数
    oracle表中有一列id她是自动增长的,插入一条数据时怎么取得id的值
  • 原文地址:https://www.cnblogs.com/heyang78/p/12763079.html
Copyright © 2020-2023  润新知