已知表中存在以下数据
Order_Auto       CUST_NO    
SL2009110001       SLM23
SL2009110002       SLM21
SL2009100001       SLQ01
SL2009100003       SLW02
Order_Auto的组成是由 CUST_NO的值前两位加上年月以及四位流水号组成,(cust_no的值是变动的)
当 cust_no前两位值是SL时,
如果当前是09年9月时, 要求得到 SL2009090001
如果当前是09年10月时,要求得到 SL2009100004
如果当前是09年11月时,要求得到 SL2009110003
当 cust_no前两位值是DE时,
如果当前是09年9月时, 要求得到  DE2009090001
如果当前是09年10月时,要求得到 DE2009100001
如果当前是09年11月时,要求得到 DE2009110001
请问用一条sql该如何实现!

解决方案 »

  1.   

    select * from [table] where substr(order_auto,3,6)=to_str(sysdate,'yyyymm');
      

  2.   

    不太清楚你的描述
    应该用case when能解决
      

  3.   

    给出的举例说明的单号和需求不是很清楚,能够列清楚一点吗?
    比如取的是最大值还是最小值?
    比较是当前月和当前CUST_NO的单号?
      

  4.   

    With a As 
    (Select 'SL2009110001' oa ,'SLM23' cn From dual Union All
    Select 'SL2009110002' oa , 'SLM21' From dual
    Union All
    Select 'SL2009100001' oa , 'SLQ01' From dual
    Union All
    Select 'SL2009100003' oa , 'SLW02' From dual
    Union All
    Select 'DE2010030003' oa , 'DEW02' From dual) 
    Select Distinct cn1, t.cn1 || to_char(Sysdate,'YYYYMM')||lpad(to_number(t.sn1+1),4,0)
    From (
    Select oa1,cn1,sy1,
    (case when oa1 = cn1||sy1 then Max(sn1) Else '0000' End) sn1  
    From (
    Select 
    substr(oa,1,8) Oa1,
    substr(cn,1,2) cn1, 
    substr(oa,9,4) sn1 ,
    to_char(Sysdate,'YYYYMM') sy1
    From a) 
    Group By oa1,cn1,sy1
    ) t
      

  5.   

    写个简单的触发器吧..SQL> drop table orderInfo;Table droppedSQL> 
    SQL> create table orderInfo
      2  (
      3    order_auto varchar2(22),
      4    cust_no    varchar2(22)
      5  )
      6  /Table createdSQL> 
    SQL> create or replace trigger genOrderAuto
      2    before insert on orderInfo
      3    for each row
      4  begin
      5    select concat(substr(:new.cust_no,0,2),to_char(trunc(sysdate,'dd'),'yyyyMMdd')) into :new.order_auto from dual;
      6  end;
      7  /Trigger createdSQL> insert into orderInfo values('sdfa','SLM23');1 row insertedSQL> select * from orderinfo;ORDER_AUTO             CUST_NO
    ---------------------- ----------------------
    SL20100304             SLM23SQL> commit;Commit completeSQL> 
      

  6.   

    支持#7楼  select substr(max(Order_Auto),8,12) into:aa from dual where Order_Auto like substr(:new.cust_no,0,2)  select substr(:new.cust_no,0,2)||to_char(trunc(sysdate,'dd'),'yyyyMMdd')||aa into :new.order_auto from dual;