日期:2014-05-16 浏览次数:20427 次
需求数据如下:
CREATE TABLE foo( num VARCHAR2(10)); INSERT INTO foo VALUES('0001'); INSERT INTO foo VALUES('0002'); INSERT INTO foo VALUES('0003'); INSERT INTO foo VALUES('0007'); INSERT INTO foo VALUES('0008'); INSERT INTO foo VALUES('0019'); INSERT INTO foo VALUES('0020'); INSERT INTO foo VALUES('0022');
SELECT LPAD(TO_NUMBER(A.NUM)+1, LENGTH(A.NUM), 0) AS NUM, RANK() OVER(ORDER BY A.NUM) AS NG FROM FOO A WHERE NOT EXISTS (SELECT 1 FROM FOO B WHERE TO_NUMBER(A.NUM) + 1 = TO_NUMBER(B.NUM))
SELECT LPAD(TO_NUMBER(A.NUM)-1, LENGTH(A.NUM), 0) AS NUM, RANK() OVER(ORDER BY A.NUM) AS NG FROM FOO A WHERE NOT EXISTS (SELECT 1 FROM FOO B WHERE TO_NUMBER(A.NUM) - 1 = TO_NUMBER(B.NUM))
SELECT O.NUM,P.NUM FROM ( SELECT LPAD(TO_NUMBER(A.NUM)+1, LENGTH(A.NUM), 0) AS NUM, RANK() OVER(ORDER BY A.NUM) AS NG FROM FOO A WHERE NOT EXISTS (SELECT 1 FROM FOO B WHERE TO_NUMBER(A.NUM) + 1 = TO_NUMBER(B.NUM)) ) O, ( SELECT LPAD(TO_NUMBER(A.NUM)-1, LENGTH(A.NUM), 0) AS NUM, RANK() OVER(ORDER BY A.NUM) AS NG FROM FOO A WHERE NOT EXISTS (SELECT 1 FROM FOO B WHERE TO_NUMBER(A.NUM) - 1 = TO_NUMBER(B.NUM)) )P WHERE O.NG+1 = P.NG