问题
This script will get the sequence,year,month and day from the following string... then find the missing number from the sequence part like (1111,1112,115,.. etc) the problem is that the output of this script are not correct does not DBMS the missing numbers
emp-1111_14_01_01_2141_G1
emp-1112_14_01_01_1612_G1
emp-1115_14_01_01_1109_G1
emp-1116_14_01_01_1315_G1
emp-1118_14_01_01_0910_G2
emp-1121_14_01_01_1105_G1
emp-1111_14_01_01_1120_G2
The output result should be like this
Missing number 1113
Missing number 1114
Missing number 1117
Missing number 1118
Missing number 1119
Missing number 1120
declare
v_name table1.ENAME%TYPE;
V_seq NUMBER (4);
V_Year number(2);
V_Month number (2);
V_day number (2);
max_seq number(4);
min_seq number(4);
CURSOR List_ENAME_cur IS
SELECT ENAME from table1
WHERE status = 2;
begin
FOR List_ENAME_rec IN List_ENAME_cur loop
if REGEXP_LIKE(List_ENAME_cur.ENAME,'emp[-][1-9]{4}[_][1-9]{2}[_][1-9]{2}[_][1-9]{2}[_][0-9]{4}[_][G]["1"]') then
V_seq := substr(List_ENAME_cur.ename,5,4);
V_Year := substr(List_ENAME_cur.ename,10,2);
V_Month := substr(List_ENAME_cur.ename,13,2);
V_day := substr(List_ENAME_cur.ename,16,2);
if min_seq is null or V_seq_FILENAME < min_seq then
min_seq := V_seq_FILENAME;
DBMS_OUTPUT.PUT_LINE('Missing number '||min_seq );
end if;
if max_seq is null or V_seq_FILENAME > max_seq then
max_seq := V_seq_FILENAME;
DBMS_OUTPUT.PUT_LINE('Missing number '||max_seq );
end if;
end if;
end loop;
DBMS_OUTPUT.PUT_LINE('max_seq '||max_seq||' min_seq '||min_seq);
end;
回答1:
See this example
WITH ORDH
AS (SELECT 1111 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1112 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1115 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1116 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1118 AS ORDERNO FROM DUAL
UNION ALL
SELECT 1121 AS ORDERNO FROM DUAL),
GOT_NEXT_ORDERNO
AS (SELECT ORDERNO,
LEAD ( ORDERNO ) OVER (ORDER BY ORDERNO) AS NEXT_ORDERNO
FROM ORDH)
SELECT ORDERNO + 1 AS FROM_NO, NEXT_ORDERNO - 1 AS TO_NO
FROM GOT_NEXT_ORDERNO
WHERE ORDERNO + 1 <> NEXT_ORDERNO;
FROM_NO TO_NO
---------- ----------
1113 1114
1117 1117
1119 1120
3 rows selected.
来源:https://stackoverflow.com/questions/21900092/missing-numbers-script-wont-work