表A  结构
 tableid [单据序号] 
 lines   [行号]现在我的 表A 中行号有点乱,我想每张单的行号进行排序,请问用什么办法比较好?
我不想用 游标,因为语句太多了,希望能用最简单的语句来实出 , 因我要对很多个表进行处理 .即  A01# 单  行号: 1
    A01# 单  行号: 2    B02# 单  行号: 1
    B02# 单  行号: 2
    B02# 单  行号: 3

解决方案 »

  1.   

    select tableid,lines 
      from 表A
     order by tableid,lines-----------------
    不清楚你要干什么,能再说一遍么?
      

  2.   

    你们误会我的意思了.我原来的表有点乱,如下:
    ---------------------------
        A01# 单  行号: 6
        A01# 单  行号: 9    B02# 单  行号: 1
        B02# 单  行号: 5
        B02# 单  行号: 20
    我希望更改后成为如下:
    ---------------------------
        A01# 单  行号: 1
        A01# 单  行号: 2    B02# 单  行号: 1
        B02# 单  行号: 2
        B02# 单  行号: 3
      

  3.   

    创建一个行号的索引
    CREATE INDEX idx_tablex_lineno ON tablex(lineno) ASC 
      

  4.   

    表中的记录按什么顺序存储是数据库的事情,跟你没关系。如果你要看按行号排序好的数据
    用 select * from tablex order by lineno asc
      

  5.   

    SQL> create table A (
      2  tableid char(10),
      3  lines number(6)
      4  );表已创建。SQL> insert into A values ('A01# 单',6);已创建 1 行。SQL> insert into A values ('A01# 单',9);已创建 1 行。SQL> insert into A values ('B02# 单',2);已创建 1 行。SQL> insert into A values ('B02# 单',6);已创建 1 行。SQL> insert into A values ('B02# 单',23);已创建 1 行。SQL> commit;提交完成。SQL> select * from A order by tableid,lines;TABLEID         LINES
    ---------- ----------
    A01# 单             6
    A01# 单             9B02# 单             2
    B02# 单             6
    B02# 单            23SQL> declare
      2    cursor c is select distinct tableid from a;
      3    crow c%rowtype;
      4  begin
      5    open c;
      6    loop
      7      exit when c%NotFound;
      8      fetch c into crow;
      9      update a
     10         set lines = rownum
     11       where a.tableid = crow.tableid;
     12    end loop;
     13    close c;
     14    commit;
     15* end;
     16  /PL/SQL 过程已成功完成。SQL> select * from a;TABLEID         LINES
    ---------- ----------
    A01# 单             1
    A01# 单             2B02# 单             1
    B02# 单             2
    B02# 单             3
      

  6.   

    create table B as 
    select A.*,dense_rank()over(partition by tableid order by lines) as rn
    from A;
    update A
    set A.lines=(select rn from B where B.tableid=A.tableid
    B.lines=A.lines);
    drop table B;
    这样做可能算比较麻烦的.我也暂时想不到好的方法!
      

  7.   

    update a t1 set lines = (select count(*) from a t2 where t1.TABLEID = t2.TABLEID and t2.lines <= t1.lines)
      

  8.   

    update a t1 set lines = (select count(*) from a t2 where t1.TABLEID = t2.TABLEID and t2.lines <= t1.lines)不行啊,替换后行号都变成一样的了:(
      

  9.   

    我的表增加了一个字段 rcd_id ,现在按 (寻) 的方法,更改成如下,已完成.
    update fix_entry t1 set f_lines = (select count(*) from fix_entry t2 where t1.f_TABLE_ID = t2.f_TABLE_ID and t2.f_rcd_id <= t1.f_rcd_id);谢谢大家的帮忙! 更谢谢 (寻) !:)
      

  10.   

    不行啊,替换后行号都变成一样的了:(
    ----------------------
    怎么会变一样的呢,是你哪里写错了吧SQL> select * from a;TABLEID                   LINES
    -------------------- ----------
    A01# 单                       6
    A01# 单                       9
    B02# 单                       1
    B02# 单                       5
    B02# 单                      20SQL> update a t1 set lines = (select count(*) from a t2 where t1.TABLEID = t2.TABLEID and t2.lines <= t1.lines);5 rows updatedSQL> select * from a;TABLEID                   LINES
    -------------------- ----------
    A01# 单                       1
    A01# 单                       2
    B02# 单                       1
    B02# 单                       2
    B02# 单                       3