RT,打比方说一个已经按照Salary降序排序的表格SAGA(NO varchar2(10),Salary varchar2(10)),如何确定i,使得Salary属性的前i条和为一固定值1500.或者说如何提取前i项属性和为1500的最小的i。用SQL语句来写
                                          

解决方案 »

  1.   

    create table SAGA(NO varchar2(10),Salary varchar2(10));insert into saga
         select 'a', 1000 from dual
     union all select 'b', 300 from dual
     union all select 'c', 200 from dual
     union all select 'd', 100 from dual
     union all select 'e', 100 from dual
     union all select 'f', 100 from dual;SQL> 
    SQL> select rn
      2    from (
      3          select rn, no, salary, sum(salary) over(order by rn) total_salary
      4            from (
      5                  select no, salary, rownum rn
      6                    from saga
      7                   order by salary desc
      8                 ) x
      9         )
     10   where total_salary = 1500;
     
            RN
    ----------
             3
      

  2.   

    select distinct sum_value, i
      from (select no,
                   sum(salary) over(order by salary desc) as sum_value,
                   rank() over(order by salary desc) as i
              from saga) t
     where t.sum_value = 1500;