Oracle库中有如下数据:公司编码 会计年度 会计期间 金额
110 2010 08 3874737.87
110 2010 09 4982510.01
110 2010 10 4537940.91
110 2010 11 4438718.57
111 2010 08 2277151.75
111 2010 09 3439095.2
111 2010 10 3493586.04
111 2010 11 4990850.34
113 2010 09 580822.3
113 2010 10 580822.3
102 2010 08 1048478.69
102 2010 09 1264515.2
102 2010 10 1047302.32
105 2010 08 2263450.71
105 2010 09 2284116.89
105 2010 10 1570958.22想实现以年份查询条件,返回每个公司月份最小的数据,即公司编码 会计年度 会计期间 金额
110 2010 08 3874737.87
111 2010 08 2277151.75
113 2010 09 580822.3
102 2010 08 1048478.69
105 2010 08 2263450.71注意:不是结果集月份最小的,而是按公司分组后,组内最小,如公司编码为113的,返回的和其他的不一样。不知道表述清楚了没有,请高手解答。

解决方案 »

  1.   


    select 公司编码, 会计年度, 会计期间, 金额 from(
          select 公司编码, 会计年度, 会计期间, 金额,
                 row_number()over(partition by 公司编码 order by 金额 asc) rn
          from tablename
    )
    where rn=1
      

  2.   

    select 公司编码,会计年度,会计期间,金额
    from (select 公司编码,会计年度,会计期间,金额,
    row_number() over(partition by 公司编码,会计年度 order by 会计期间) rn
    from tb)
    where rn=1
      

  3.   

    你的是公司分组后,月份最小的?还是公司分组后,金额最小的?
    上面那个事金额最小的,
    下面这个事月份最小的:select 公司编码, 会计年度, 会计期间, 金额 from(
          select 公司编码, 会计年度, 会计期间, 金额,
                 row_number()over(partition by 公司编码 order by 会计年度, 会计期间) rn
          from tablename
    )
    where rn=1
      

  4.   

    --orselect 公司编码,会计年度,会计期间,金额
    from tb a
    where not exists(select 1 from tb b where a.公司编码=b.公司编码 and a.会计年度=b.会计年度
    and a.会计期间>b.会计期间)
      

  5.   

    -- 公司编码 会计年度 会计期间 金额SELECT t1.公司编码, t1.会计年度, t1.会计期间, t1.金额
    FROM table_name t1
    WHERE exists (select t2.公司编码, min(t2.金额) FROM table_name t2 
                  where t2.公司编码=t1.公司编码
                    group by t2.公司编码
                    having min(t2.金额)=t1.金额 );