在oracle中求一sql

快跑蜗牛哥 2010-03-04 01:39:46
已知表中存在以下数据
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该如何实现!
...全文
106 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
banana_97 2010-03-08
  • 打赏
  • 举报
回复
支持#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;
YY_MM_DD 2010-03-04
  • 打赏
  • 举报
回复
写个简单的触发器吧..

SQL> drop table orderInfo;

Table dropped

SQL>
SQL> create table orderInfo
2 (
3 order_auto varchar2(22),
4 cust_no varchar2(22)
5 )
6 /

Table created

SQL>
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 created

SQL> insert into orderInfo values('sdfa','SLM23');

1 row inserted

SQL> select * from orderinfo;

ORDER_AUTO CUST_NO
---------------------- ----------------------
SL20100304 SLM23

SQL> commit;

Commit complete

SQL>
cyousor 2010-03-04
  • 打赏
  • 举报
回复
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
langcai1981 2010-03-04
  • 打赏
  • 举报
回复
给出的举例说明的单号和需求不是很清楚,能够列清楚一点吗?
比如取的是最大值还是最小值?
比较是当前月和当前CUST_NO的单号?
cyousor 2010-03-04
  • 打赏
  • 举报
回复
年月,是从什么地方来的
小灰狼W 2010-03-04
  • 打赏
  • 举报
回复
不太清楚你的描述
应该用case when能解决
yanyue2008 2010-03-04
  • 打赏
  • 举报
回复
select * from [table] where substr(order_auto,3,6)=to_str(sysdate,'yyyymm');
cyousor 2010-03-04
  • 打赏
  • 举报
回复
四位流水号 ,生成规则是什么???

17,089

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧