select comid,y_month,m_month,cost_all,count_bad,comtroubledid , 
       func(y_month,m_month,comid) as row_no
from t1
order by comid , y_month , m_month desc You have to create a function func to calculate the row_no and call it in your SQL

解决方案 »

  1.   

    Definitly there are some other method , but it would be a quite complicated SQL . 
    I think solving the problem is more important then writing a unreadable SQL :-)
      

  2.   

    use olap function:
    rank() over (order by ...)
    you can learn this from sql reference
      

  3.   

    试试这个:select b.comid,b.y_month,b.m_month, b.COST_ALL,b.COUNT_BAD,b.COMTROUBLEID,a.row_no
    (select rownum row_no, comid,y_month,m_month
     from yourtable
     group by rownum,comid,y_month,m_month
    ) a, yourtable b
    where  a.comid=b.comid and a.y_month=b.y_month and a.m_month=b.m_month
    order by b.count_bad
      

  4.   

    Sorry ,应该是select b.comid,b.y_month,b.m_month, b.COST_ALL,b.COUNT_BAD,b.COMTROUBLEID,a.row_no
    ( select rownum row_no,comid,y_month,m_month from
     (select distinct comid,y_month,m_month from yourtable) c 
    )a , yourtable b
    where  a.comid=b.comid and a.y_month=b.y_month and a.m_month=b.m_month
    order by b.count_bad
      

  5.   

    create or replace function get(p_y_month in varchar2,p_m_month in varchar2,p_comid in varchar2,p_count_bad varchar2) 
    return number 
    as
    cursor t_sor(v_y_month varchar2,v_m_month varchar2,v_comid varchar2) is
    select rownum rm,a.* from t1 where comid=v_y_month and y_month=v_m_month and m_month=v_comid;
    begin
    for v_sor in t_sor(p_y_month ,p_m_month ,p_comid) loop
    if v_sor.count_bad=p_count_bad then
      return(v_sor.rm);
    end if;
    end loop;
    end;
    /select comid,y_month,m_month,cost_all,count_bad,comtroubledid , 
           get(y_month,m_month,comid,count_bad) as row_no
    from t1
    order by comid , y_month , m_month,COUNT_BAD  desc
      

  6.   

    如果楼主的数据库是8i或8i以上,完全没有必要这么复杂,又是过程,又是函数的。
    完全可以利用分析函数去解决楼主的问题,不然,oracle为什么要设计出分析函数呢!
    同意(西域浪子)的做法。
      

  7.   

    在SQL *plus中测试:select rank() over (order by componentsid,yeildmonth,maintainmonth,count_bad desc) as row_no,
    *
    ERROR 位于第 1 行:
    ORA-00439: 未启用特征: OLAP Window Functions不知道要怎么启用联机分析处理?
    使用这个函数为什么要启用OLAP?有什么关系吗?
      

  8.   

    to black_snail(●龙飞虎○) :
    it's too slow to use func;