zoukankan      html  css  js  c++  java
  • 经典SQL问题:Top 10%

    学生表:

    create table hy_student(
       id number(4,0) primary key,
       name nvarchar2(20) not null,
       score number(3,0) not null)

    充值:

    insert into hy_student
    select rownum,dbms_random.string('*',dbms_random.value(1,20)),dbms_random.value(0,100)
    from dual
    connect by level<201
    order by dbms_random.random

    第一步把学生按成绩逆序排列:

    select * from hy_student order by score desc

    第二步给加伪列:

    select rownum as rn,a.* from (select * from hy_student order by score desc) a

    最后就可以检出前10%的精英了:

    select b.* from (select rownum as rn,a.* from (select * from hy_student order by score desc) a) b where b.rn<= (select count(*) from hy_student)/10

    结果:

    --2020-04-02--

    以上用到的全部SQL:

    create table hy_student(
       id number(4,0) primary key,
       name nvarchar2(20) not null,
       score number(3,0) not null)
       
    insert into hy_student
    select rownum,dbms_random.string('*',dbms_random.value(1,20)),dbms_random.value(0,100)
    from dual
    connect by level<201
    order by dbms_random.random
    
    commit;
    
    select * from hy_student order by score desc
    
    select rownum as rn,a.* from (select * from hy_student order by score desc) a
    
    select b.* from (select rownum as rn,a.* from (select * from hy_student order by score desc) a) b where b.rn<= (select count(*) from hy_student)/10
  • 相关阅读:
    HDU 4388 To the moon
    HDU 4757 Tree
    HDU 5816 Hearthstone
    hihocoder 1356 分隔相同整数
    HDU 5726 GCD
    POJ3026 Borg Maze(Prim)(BFS)
    POJ1258 Agri-Net(Prim)
    POJ1751 Highways(Prim)
    POJ2349 Arctic Network(Prim)
    POJ1789 Truck History(Prim)
  • 原文地址:https://www.cnblogs.com/heyang78/p/12619631.html
Copyright © 2011-2022 走看看