已知表中存在以下数据
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该如何实现!
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该如何实现!
解决方案 »
- timestamp 如何更新数据
- 找数据库练习题
- 求助 ORA-12154: TNS:could not resolve the connect identifier specified
- ORACLE 行列转换问题
- 请问为什么我运行sqlplus时报错message file sp1<lang>.msb not found
- 如何禁止Oracle用户从PL/SQL Developer应用程序访问数据库?
- ORACLE 9i数据库优化,statspack报告,请大侠救命呀。
- 一个年季月查询数据的问题
- 我想把oracle10g中的数据导入到oracle9i中去,应该怎么做呢?
- 如何获得表空间中的所有对象?
- 异地数据库数据同步
- sql查询转置
应该用case when能解决
比如取的是最大值还是最小值?
比较是当前月和当前CUST_NO的单号?
(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
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>