table  a
parent_cd  char(5)
child_cd   char(8)
child_heji     number(10)
parent_heji  number(10)数据
parent_cd   child_cd  child_heji  parent_heji
77777       11111111     500      1000
77777       11111112     400      1000
77777       11111113     550      1000
77777       11111114     600      1000
77777       11111115     230      1000
87777       21111111     500      3000
87777       21111112     400      3000
87777       21111113     550      3000
87777       21111114     600      3000要求 parent_heji的 top 50 中 的 child_heji的 top 3的数据 结果parent_cd   child_cd  child_heji  parent_heji
77777       11111114     600      1000
77777       11111113     550      1000
77777       11111111     500      100087777       21111114     600      3000
87777       21111113     550      3000
87777       21111111     500      3000

解决方案 »

  1.   

    用子查询实现吧
    select ...
    from 
    (select ...
    from 
    tab 
    where ...)
    where ...
      

  2.   

    说的不够清楚,是按什么分类进行去取呀
    "要求 parent_heji的 top 50 中 的 child_heji的 top 3的数据 "
    如果按这句话去理解
    select * 
    from(
    select * from
    (select *
    from(
    select * from table order by parent_heji desc) where rownum<=50
    ) order by child_heji desc) where rownum<=3但结果肯定不是你给出的例子结果
      

  3.   

    按照 parent_heji 分类
    在 parent_heji中 top 50 的 child_heji 在 进行分类 ,
    各parent_heji 中的 ,child_heji 的 top 50 数据显示 。看看结果。
      

  4.   


    看来,是 说的不够清楚。就是 先 按照  parent_heji 求出 parent_heji 中 top 50 的 distinct  parent_heji ,
    最多 parent_heji  个数 是 50 个 ,然后, 在 top 50 的 parent_heji  中, 查询,child_heji 中 top 50 的数据 。最终的结果是 parent_heji  *  child_heji  = 50 * 50 = 2500 行 数据。
      

  5.   

    child_heji的 top 3的数据
    =======================
    parent_heji  *  child_heji(top3)  <= 50 * 3 <= 150 行 数据?select *
    from(
    select dense_rank(order by parent_heji ) pn,
           row_number(partition by parent_heji order by child_heji) cn,
           a.*
    from a
    )
    where pn<51
      and cn<4数据多时就先把前50条的parent_heji取出来
      

  6.   

    create table t_05(parent_cd varchar2(10),child_cd varchar2(10),child_heji number(8,2),parent_heji number(8,2));insert into t_05 values('77777','11111111',500,1000); 
    insert into t_05 values('77777','11111112',400,1000); 
    insert into t_05 values('77777','11111113',550,1000); 
    insert into t_05 values('77777','11111114',600,1000); 
    insert into t_05 values('77777','11111115',230,1000); 
    insert into t_05 values('87777','21111111',500,3000); 
    insert into t_05 values('87777','21111112',400,3000); 
    insert into t_05 values('87777','21111113',550,3000); 
    insert into t_05 values('87777','21111114',600,3000); select parent_cd,child_cd,child_heji,parent_heji from 
    (select parent_cd,child_cd,child_heji,parent_heji,
    dense_rank() over(partition by parent_cd order by child_heji desc) rn
    from
    (
    select
    parent_cd ,child_cd ,child_heji,parent_heji,
    dense_rank() over(partition by parent_cd order by parent_heji desc) rn 
    from t_05) where rn<=50) where rn<=3
    order by parent_cd,rnPARENT_CD  CHILD_CD   CHILD_HEJI PARENT_HEJI
    ---------- ---------- ---------- -----------
    77777      11111114          600        1000
    77777      11111113          550        1000
    77777      11111111          500        1000
    87777      21111114          600        3000
    87777      21111113          550        3000
    87777      21111111          500        3000已选择6行。