请问 下面的表该如何排序?我有下面的表。次序如下 其中第5个记录和第6条记录次序颠倒了。这个表数据量比较大。 用什么方法可以对这个表中的  b列排序。使其能够按照升序排列。 
a        b 
1 #1杆塔 
2 #2杆塔 
3 #3.1杆塔 
4 #3.2杆塔 
5 #5杆塔 
6 #4杆塔 
7 #6杆塔 
8 #7杆塔 
9 #9杆塔
10 #8杆塔现在要做的事情就是把 次序不对的列找出来。 不是排序。比如上列中b列第5,第6列。第9,10次序不对。 现在要把其中的列找出来。
5 #5杆塔 
6 #4杆塔 
9 #9杆塔
10 #8杆塔
就是把这样的列能够找出来。

解决方案 »

  1.   

    with  temp 
    as 
    (
    select 1 b,'#1杆塔'a from dual
    union all
    select 2,'#2杆塔' from dual
    union all 
    select  3,'#3.1杆塔' from dual
    union all 
    select 4,'#3.2杆塔' from dual
    union all 
    select  5,'#5杆塔' from dual
    union all 
    select  6,'#4杆塔' from dual
    union all 
    select  7,'#6杆塔' from dual
    union all 
    select  8,'#7杆塔' from dual
    )
    select a,b  
    from 
    (select a,b,row_number() over(partition by 1 order by a)  rn from temp order by a,b)
    where b<>rn
    --result:#4杆塔 6
    #5杆塔 5
      

  2.   

    with  temp 
    as 
    (
    select 1 b,'#1杆塔'a from dual
    union all
    select 2,'#2杆塔' from dual
    union all 
    select  3,'#3.1杆塔' from dual
    union all 
    select 4,'#3.2杆塔' from dual
    union all 
    select  5,'#5杆塔' from dual
    union all 
    select  6,'#4杆塔' from dual
    union all 
    select  7,'#6杆塔' from dual
    union all 
    select  8,'#7杆塔' from dual
    union all
    select  9,'#9杆塔' from dual
    union all
    select  10,'#8杆塔' from dual
    )
    select a,b  
    from 
    (select a,b,row_number() over(partition by 1 order by a)  rn from temp order by a,b)
    where b<>rn
    --result:#4杆塔 6
    #5杆塔 5
    #8杆塔 10
    #9杆塔 9
      

  3.   

    SQL> with temp as(
      2  select 1 a, '#1杆塔' b from dual union all
      3  select 2 a, '#2杆塔' b from dual union all
      4  select 3 a, '#3.1杆塔' b from dual union all
      5  select 4 a, '#3.2杆塔' b from dual union all
      6  select 5 a, '#5杆塔' b from dual union all
      7  select 6 a, '#4杆塔' b from dual
      8  )  select a, b from (select temp.*, row_number() over(order by a asc) a_ord,r
    ow_number() over(order by to_number(substr(b, 2, length(b)-3)) asc) b_ord from t
    emp) t where t.a_ord<>t.b_ord;         A B
    ---------- --------
             6 #4杆塔
             5 #5杆塔
      

  4.   

    同意上面的,我这里的第一个a的 row_number()没有意义。
      

  5.   

    --row_number()函数是SQL Server 2005里面的,不是Oracle的
      

  6.   


    SQL> select * from t;                                      A B
    --------------------------------------- --------------------
                                          1 #1¸ËËþ
                                          2 #2¸ËËþ
                                          3 #3.1¸ËËþ
                                          4 #3.2¸ËËþ
                                          5 #5¸ËËþ
                                          6 #4¸ËËþ
                                          7 #6¸ËËþ
                                          8 #7¸ËËþ
                                          9 #9¸ËËþ
                                         10 #8¸ËËþ10 rows selectedSQL> 
    SQL> with tt as (
      2     select a, b,c-lag(c,1,0)over(order by a) d,lag(c)over(order by a) e from (
      3     select a,b,replace(replace(b,'#',''),'¸ËËþ','') c from t
      4    )
      5  )
      6  select a,b from tt where tt.a in(
      7    select a from tt where d<0
      8    union
      9    select cast(e as number) from tt where d<0
     10  );                                      A B
    --------------------------------------- --------------------
                                          5 #5¸ËËþ
                                          6 #4¸ËËþ
                                          9 #9¸ËËþ
                                         10 #8¸ËËþSQL> 
      

  7.   


    SQL> select * from t;                                      A B
    --------------------------------------- --------------------
                                          1 #1杆塔
                                          2 #2杆塔
                                          3 #3.1杆塔
                                          4 #3.2杆塔
                                          5 #5杆塔
                                          6 #4杆塔
                                          7 #6杆塔
                                          8 #7杆塔
                                          9 #9杆塔
                                         10 #8杆塔10 rows selectedSQL> 
    SQL> with tt as (
      2     select a, b,c-lag(c,1,0)over(order by a) d,lag(c)over(order by a) e from (
      3     select a,b,replace(replace(b,'#',''),'杆塔','') c from t
      4    )
      5  )
      6  select a,b from tt where tt.a in(
      7    select a from tt where d<0
      8    union
      9    select cast(e as number) from tt where d<0
     10  );                                      A B
    --------------------------------------- --------------------
                                          5 #5杆塔
                                               6 #4杆塔
                                               9 #9杆塔
                                               10 #8杆塔SQL> 
      

  8.   

    ORA-01790:表达式必须具有与对应表达式相同的数据类型
     用楼上的再运行的时候报上面的错误。我的2个字段都是varchar2(50)的。
      

  9.   

    用#3楼的,但是修改一下with temp as( 
      select 1 a, '#1杆塔' b from dual union all 
      select 2 a, '#2杆塔' b from dual union all 
      select 3 a, '#3.1杆塔' b from dual union all 
      select 4 a, '#3.2杆塔' b from dual union all 
      select 5 a, '#5杆塔' b from dual union all 
      select 6 a, '#4杆塔' b from dual 
      )  
      
      select a, b from (select temp.*, row_number() over(order by a asc) a_ord,
      row_number() over(partition by 1 order by to_number(substr(b, 2, length(b)-3)) asc) b_ord from temp) t where t.a_ord <>t.b_ord; 
      

  10.   

    是oracl先有了,sql server再用的
      

  11.   

    12楼不是写的很好了么,上下两条对比,楼主的表用varchar型是对比会有问题的,系统会认为11比2小