请教一个问题,有一列存放的是字符型数据,
如:231
   628
   173
如何实现按数字顺序排序
结果为:
   123
   268
   137

解决方案 »

  1.   


    with t1 as
    (
         select 1 id,231 B from dual
         union all
         select 2 id,628 B from dual
         union all
         select 3 id,173 B from dual
    )select id,replace(wm_concat(b),',','') b
    from 
        (select id,b from
            (select id,substr(b,1,1) b from t1
            union all
            select id,substr(b,2,1) from t1
            union all
            select id,substr(b,3,1) from t1
            )
        order by id,b)
    group by id 
         id      b
    ------------------------
    1 1 123
    2 2 268
    3 3 173
      

  2.   

    没太明白,你的写法,我的表是 a3d1000,字段是code这个字段,这个怎么改法
      

  3.   

    with t1 as
    (
        select CODE ,rownum id,code B from a3d1000
    /*     select 1 id,231 B from dual
         union all
         select 2 id,628 B from dual
         union all
         select 3 id,173 B from dual*/
    )select id,replace(wm_concat(b),',','') b
    from 
        (select id,b from
            (
            
            select id,to_number(substr(b,1,1)) b from t1
            union all
            select id,to_number(substr(b,2,1)) from t1
            union all
            select id,to_number(substr(b,3,1)) from t1
            )
        order by id,b)
    group by id有的没有转过来,如下;185
    178
      

  4.   


      表名替换掉t1 code替换B  表中主键或者唯一字段替换id
      wm_concat 用来合并数据,以逗号来隔开 
    --如:表t1  字段c1  有3行数据
    aa
    bb
    ccselect wm_concat(c1) c1
    from t1 c1
    ----------
    aa,bb,cc
      
      

  5.   

    可能是函数wm_concat失效吧  第一次碰到没按顺序组合的情况  下面换了个方式来判断 不知道写麻烦没create table a3d1000 (id number(10),code varchar2(10));insert into a3d1000 values (1,'231');
    insert into a3d1000 values (2,'628'); 
    insert into a3d1000 values (3,'185');
    insert into a3d1000 values (4,'178');
    insert into a3d1000 values (5,'173');
    commit;select t1.id,x||replace(replace(t2.code,x,''),y,'')||y code
    from
      (select id,LEAST(LEAST(c1,c2),c3) x,greatest(greatest(c1,c2),c3) y
      from
          (select id,substr(code,1,1) c1, substr(code,2,1) c2, substr(code,3,1) c3
          from a3d1000
          )
      ) t1,a3d1000 t2
    where t1.id=t2.id
         id    code
    -------------------------------
    1 1 123
    2 2 268
    3 3 158
    4 4 178
    5 5 137
      

  6.   

    再加个判断select t1.id,x||(case when x=replace(t2.code,y,'') then y when y=replace(t2.code,x,'') then x else replace(replace(t2.code,x,''),y,'') end)||y code
    from
      (select id,LEAST(LEAST(c1,c2),c3) x,greatest(greatest(c1,c2),c3) y
      from
          (select id,substr(code,1,1) c1, substr(code,2,1) c2, substr(code,3,1) c3
          from a3d1000
          )
      ) t1,a3d1000 t2
    where t1.id=t2.id
      

  7.   


    create table a3d1000 (id number(10),code varchar2(10));insert into a3d1000 values (1,'231');
    insert into a3d1000 values (2,'628'); 
    insert into a3d1000 values (3,'185');
    insert into a3d1000 values (4,'178');
    insert into a3d1000 values (5,'173'); 
    insert into a3d1000 values (6,'334');
    insert into a3d1000 values (7,'111');
    commit;select t1.id,x||(case when replace(t2.code,y,'') is null then x  
                          when x=replace(t2.code,y,'') then y 
                          when y=replace(t2.code,x,'') then x 
                          else replace(replace(t2.code,x,''),y,'') end)||y code
    from
      (select id,LEAST(LEAST(c1,c2),c3) x,greatest(greatest(c1,c2),c3) y
      from
          (select id,substr(code,1,1) c1, substr(code,2,1) c2, substr(code,3,1) c3
          from a3d1000
          )
      ) t1,a3d1000 t2
    where t1.id=t2.id
         id    code
    ------------------------
    1 1 123
    2 2 268
    3 3 158
    4 4 178
    5 5 137
    6 6 334
    7 7 111