本帖最后由 w78111909 于 2010-07-23 10:23:09 编辑

解决方案 »

  1.   

    select * from (
    select jh,rq,rcy,row_number()over(partition by jh order by rcy desc,rq)n from (
    select 'BJ12' jh, '2010/07/20' rq, 200 rcy  from dual 
    union all
    select 'BJ12' jh, '2010/07/21' rq, 300 rcy  from dual
    union all
    select 'BJ12' jh, '2010/07/22' rq, 100 rcy  from dual
    union all
    select 'BJ12' jh, '2010/07/23' rq, 300 rcy  from dual
    union all
    select 'SH10' jh, '2010/07/20' rq, 150 rcy  from dual
    union all
    select 'SH10' jh, '2010/07/22' rq, 150 rcy  from dual
    union all
    select 'TJ20' jh, '2010/07/23' rq, 120 rcy  from dual
    )) where n=1
      

  2.   

    row_number ,开窗 分组 排序 
      

  3.   

    跟表关联之后,再用row_number 一样的
      

  4.   


    with DBA01
     as (select 'BJ12' jh, '2010/07/20' rq, 200 rcy from dual  
    union all
    select 'BJ12' jh, '2010/07/21' rq, 300 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/22' rq, 100 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/23' rq, 300 rcy from dual
    union all
    select 'SH10' jh, '2010/07/20' rq, 150 rcy from dual
    union all
    select 'SH10' jh, '2010/07/22' rq, 150 rcy from dual
    union all
    select 'TJ20' jh, '2010/07/23' rq, 120 rcy from dual
    ),
    DAA01
    as ( select  'BJ12' jh , '2010/07/20' tcrq from dual
    union all 
    select  'SH10' jh , '2010/07/20' tcrq from dual
    union all
    select  'TJ20' jh , '2010/07/23' tcrq from dual
    )
    SELECT DBA01.JH,DBA01.RQ,DBA01.RCY FROM DBA01,
    (select DBA01.JH,DBA01.RQ,ROW_NUMBER() OVER(PARTITION BY DBA01.JH ORDER BY DBA01.RCY DESC,DBA01.RQ ASC) RN
    from DBA01,DAA01 WHERE DAA01.JH=DBA01.JH ) TT WHERE DBA01.JH=TT.JH AND DBA01.RQ=TT.RQ AND TT.RN=1 ; 
      

  5.   

    select * from (
    select t1.jh,t2.rq,t2.rcy,row_number()over(partition by t1.jh order by t2.rcy desc,t2.rq)n from 
    (
    select 'BJ12' jh, '2010/07/20' TCRQ  from dual
    union all
    select 'SH10' jh, '2010/07/20' TCRQ  from dual
    union all
    select 'TJ20' jh, '2010/07/23' TCRQ  from dual  
    )t1,(
    select 'BJ12' jh, '2010/07/20' rq, 200 rcy  from dual 
    union all
    select 'BJ12' jh, '2010/07/21' rq, 300 rcy  from dual
    union all
    select 'BJ12' jh, '2010/07/22' rq, 100 rcy  from dual
    union all
    select 'BJ12' jh, '2010/07/23' rq, 300 rcy  from dual
    union all
    select 'SH10' jh, '2010/07/20' rq, 150 rcy  from dual
    union all
    select 'SH10' jh, '2010/07/22' rq, 150 rcy  from dual
    union all
    select 'TJ20' jh, '2010/07/23' rq, 120 rcy  from dual
    )t2 where t1.jh = t2.jh) where n=1
      

  6.   


    还能优化吗~?我的DBA01中有1千万条数据,而分类表中只有DAA01有1百来条,我只想统计DBA01中今年的数据,用RQ怎么来加条件
      

  7.   

    感觉只要DBA01就可以了?
    WITH DBA01
     as (select 'BJ12' jh, '2010/07/20' rq, 200 rcy from dual  
    union all
    select 'BJ12' jh, '2010/07/21' rq, 300 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/22' rq, 100 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/23' rq, 300 rcy from dual
    union all
    select 'SH10' jh, '2010/07/20' rq, 150 rcy from dual
    union all
    select 'SH10' jh, '2010/07/22' rq, 150 rcy from dual
    union all
    select 'TJ20' jh, '2010/07/23' rq, 120 rcy from dual
    )
    SELECT *
      FROM (SELECT JH,
                   RQ,
                   RCY,
                   ROW_NUMBER() OVER(PARTITION BY JH ORDER BY RCY DESC) RN
              FROM DBA01) WHERE rn=1
      

  8.   

    这个快一点:with DBA01
     as (select 'BJ12' jh, '2010/07/20' rq, 200 rcy from dual  
    union all
    select 'BJ12' jh, '2010/07/21' rq, 300 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/22' rq, 100 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/23' rq, 300 rcy from dual
    union all
    select 'SH10' jh, '2010/07/20' rq, 150 rcy from dual
    union all
    select 'SH10' jh, '2010/07/22' rq, 150 rcy from dual
    union all
    select 'TJ20' jh, '2010/07/23' rq, 120 rcy from dual
    ),
    DAA01
    as ( select  'BJ12' jh , '2010/07/20' tcrq from dual
    union all 
    select  'SH10' jh , '2010/07/20' tcrq from dual
    union all
    select  'TJ20' jh , '2010/07/23' tcrq from dual
    )
    SELECT DBA01.JH,DBA01.RQ,DBA01.RCY FROM DBA01,
    (select DBA01.JH,DBA01.RQ,ROW_NUMBER() OVER(PARTITION BY DBA01.JH ORDER BY DBA01.RCY DESC,DBA01.RQ ASC) RN
    from DBA01 WHERE exists (select null from DAA01 where DAA01.JH=DBA01.JH and to_number(substr(DAA01.tcrq,1,4))=2010)) TT 
    WHERE DBA01.JH=TT.JH AND DBA01.RQ=TT.RQ AND TT.RN=1 ; 
      

  9.   


    WITH DBA01
     as (select 'BJ12' jh, '2010/07/20' rq, 200 rcy from dual  
    union all
    select 'BJ12' jh, '2010/07/21' rq, 300 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/22' rq, 100 rcy from dual
    union all
    select 'BJ12' jh, '2010/07/23' rq, 300 rcy from dual
    union all
    select 'SH10' jh, '2010/07/20' rq, 150 rcy from dual
    union all
    select 'SH10' jh, '2010/07/22' rq, 150 rcy from dual
    union all
    select 'TJ20' jh, '2010/07/23' rq, 120 rcy from dual
    )
    select * from DBA01 a
    where not exists (select 1 from DBA01 where jh=a.jh and rcy<a.rcy)