有三个单位,每个单位各有前十名,现在要将想取出三个单位合计的前十名并删除其他的二十名,求SQL
表结构如下表A
unit_id    单位代码  key,
unit_name  单位名称,
man_id     个人代码  key,
man_name   个人名称,
money      钱

解决方案 »

  1.   

    取总的前十名:
    select * 
    from your_table
    where rownum <11
    order by money;
      

  2.   

    delete from (select * from unit1 union all select * from unit2 union all select * from unit3)a where man_id not in (select top 20 man_id from a order by money)
      

  3.   

    提供个不是很好的给你
    select * from 
    (select * from your_table where 第一个单位 and rownum <11 order by money
    union
    select * from your_table where 第二个单位 and rownum <11 order by money
    union
    select * from your_table where 第三个单位 and rownum <11 order by money)
    where rownum <11 order by money
      

  4.   

    select * from 
    (select * from your_table where 第一个单位 and rownum <11 order by money desc
    union
    select * from your_table where 第二个单位 and rownum <11 order by money desc
    union
    select * from your_table where 第三个单位 and rownum <11 order by money desc)
    where rownum <11 order by money desc
      

  5.   

    select * from 
    (select * from your_table where 第一个单位 and rownum <11 order by money desc
    union
    select * from your_table where 第二个单位 and rownum <11 order by money desc
    union
    select * from your_table where 第三个单位 and rownum <11 order by money desc)
    where rownum <11 order by money desc
      

  6.   

    忘了rownum是比order先执行的,所以正确的应该是下面的
    select * from 
    (select * from 
    (select * from (select * from your_table where 第一个单位 order by money desc) where rownum < 11
    union all
    select * from (select * from your_table where 第二个单位 order by money desc) where rownum < 11
    union all
    select * from (select * from your_table where 第三个单位 order by money desc) where rownum < 11)
    order by money desc)
    where rownum < 11
      

  7.   

    就一张表吗,那很好处理的select * from A where rownum <=10  order by moneydelete from A where man_id not in (select man_id from A where rownum <=10  order by money)