# Finding gaps with analytic functions

Finding gaps is classic problem in PL/SQL. The basic concept is that you have some sort of numbers (like these: 1, 2, 3, 5, 6, 8, 9, 10, 15, 20, 21, 22, 23, 25, 26), where there’s supposed to be a fixed interval between the entries, but some entries could be missing. The gaps problem involves identifying the ranges of missing values in the sequence. For these numbers, the solution will be as follows:

START_GAP END_GAP

4 4

7 7

11 14

16 19

24 24

First, run the following code, to create tab1 table:

CREATE TABLE tab1

(

col1 INTEGER

);

Then, insert a few rows:

INSERT INTO tab1 VALUES (1);

INSERT INTO tab1 VALUES (2);

INSERT INTO tab1 VALUES (3);

INSERT INTO tab1 VALUES (5);

INSERT INTO tab1 VALUES (6);

INSERT INTO tab1 VALUES (8);

INSERT INTO tab1 VALUES (9);

INSERT INTO tab1 VALUES (10);

INSERT INTO tab1 VALUES (15);

INSERT INTO tab1 VALUES (20);

INSERT INTO tab1 VALUES (21);

INSERT INTO tab1 VALUES (22);

INSERT INTO tab1 VALUES (23);

INSERT INTO tab1 VALUES (25);

INSERT INTO tab1 VALUES (26);

COMMIT;

With data, you can take care of solving the gaps problem…

One of the most efficient solutions to the gaps problem involves using analytic functions (also known as window functions)

WITH aa AS

(SELECT col1 AS cur_value, LEAD (col1) OVER (ORDER BY col1) AS next_value

FROM tab1)

SELECT cur_value + 1 AS start_gap, next_value - 1 AS end_gap

FROM aa

WHERE next_value - cur_value > 1

ORDER BY start_gap

Using the LEAD function, you can return for each current col1 value (call it cur_value) the next value in the sequence (call it next_value). Then you can filter only pairs where the difference between the two is greater than the one.

- learn-with-video-tutorial's blog
- Login to post comments