新人刚接触oracle,遇到难题希望高手指教!
在一张表中有订单号和运单号两个栏位,格式如下:
ordercode        shiptocode
- - - - - - - - - - - - - - - - - -
AA975773.01       BB00001
AA975773.02       BB00002
AA975773.03       BB00003
AA975773.04       BB00004
AA975773.04       BB00005
AA231431.01       BB00011
AA231431.02       BB00012
AA231431.03       BB00013
AA231431.04       BB00014
……              ………
现希望选出这样的一个新栏位,如果ordercode是主订单,即以.01结尾,则新栏位等于shiptocode,如果是副订单,即以其他数字结尾,则新栏位等于主订单的shiptocode。即选出如下格式:
ordercode        shiptocode         new
- - - - - - - - - - - - - - - - - - - - - - -
AA975773.01       BB00001           BB00001
AA975773.02       BB00002           BB00001   
AA975773.03       BB00003           BB00001
AA975773.04       BB00004           BB00001
AA975773.04       BB00005           BB00001
AA231431.01       BB00011           BB00011
AA231431.02       BB00012           BB00012
AA231431.03       BB00013           BB00013
AA231431.04       BB00014           BB00014
……              ………            ………
我只想到前一步,即:select case when ordercode like '%.01' then shiptocode
else....
下一步卡住了希望各位前辈给个思路~多谢!
 

解决方案 »

  1.   

    依你的描述,应该是想要这样的结果吧
    ordercode shiptocode new
    - - - - - - - - - - - - - - - - - - - - - - -
    AA975773.01 BB00001 BB00001
    AA975773.02 BB00002 BB00001   
    AA975773.03 BB00003 BB00001
    AA975773.04 BB00004 BB00001
    AA975773.04 BB00005 BB00001
    AA231431.01 BB00011 BB00011
    AA231431.02 BB00012 BB00011
    AA231431.03 BB00013 BB00011
    AA231431.04 BB00014 BB00011
      

  2.   

    先这样吧,试试符不符合with a as
    (
    select 'AA975773.01' ordercode,'BB00001' shiptocode from dual union all
    select 'AA975773.02','BB00002' from dual union all
    select 'AA975773.03','BB00003' from dual union all
    select 'AA975773.04','BB00004' from dual union all
    select 'AA975773.04','BB00005' from dual union all
    select 'AA231431.01','BB00011' from dual union all
    select 'AA231431.02','BB00012' from dual union all
    select 'AA231431.03','BB00013' from dual union all
    select 'AA231431.04','BB00014' from dual 
    )
    select ordercode
           ,shiptocode
           ,min(shiptocode) over(partition by substr(ordercode,1,instr(ordercode,'.')-1))
    from a 
    ;
    ORDERCODE   SHIPTOC MIN(SHI
    ----------- ------- -------
    AA231431.03 BB00013 BB00011
    AA231431.02 BB00012 BB00011
    AA231431.01 BB00011 BB00011
    AA231431.04 BB00014 BB00011
    AA975773.04 BB00004 BB00001
    AA975773.03 BB00003 BB00001
    AA975773.02 BB00002 BB00001
    AA975773.01 BB00001 BB00001
    AA975773.04 BB00005 BB00001
      

  3.   

    呵呵~谢谢你的query,可是这个方法可能不行,因为要选出来的一共有20多个栏位,只不过要通过这两个栏位新增加一个,所以我只列出这3个,如果用这个方法的话最后query的performance就比较差了,而且shiptocode这个栏位其实也并不是规律的,所以用最小来筛选可能不行,别人给我这个东西的时候大概说到用判断做,麻烦再往这个思路帮我考虑下~
      

  4.   


    with a as
    (
    select 'AA975773.01' ordercode,'BB00001' shiptocode from dual union all
    select 'AA975773.02','BB00002' from dual union all
    select 'AA975773.03','BB00003' from dual union all
    select 'AA975773.04','BB00004' from dual union all
    select 'AA975773.04','BB00005' from dual union all
    select 'AA231431.01','BB00011' from dual union all
    select 'AA231431.02','BB00012' from dual union all
    select 'AA231431.03','BB00013' from dual union all
    select 'AA231431.04','BB00014' from dual 
    )
    select a.ordercode
           ,a.shiptocode
           ,b.shiptocode
    from a, 
    (
    select ordercode
           ,shiptocode
           ,row_number() over(partition by substr(ordercode,1,instr(ordercode,'.')-1) order by ordercode) rn       
    from a 
    )b
    where substr(a.ordercode,1,instr(a.ordercode,'.')-1)=substr(b.ordercode,1,instr(b.ordercode,'.')-1)
    and b.rn=1
    ;
    ORDERCODE   SHIPTOC SHIPTOC
    ----------- ------- -------
    AA231431.04 BB00014 BB00011
    AA231431.03 BB00013 BB00011
    AA231431.02 BB00012 BB00011
    AA231431.01 BB00011 BB00011
    AA975773.04 BB00005 BB00001
    AA975773.04 BB00004 BB00001
    AA975773.03 BB00003 BB00001
    AA975773.02 BB00002 BB00001
    AA975773.01 BB00001 BB00001