I have a table containing numbers (phone numbers) and a code (free or inaccessible).
Now I need to find a series of 30 consecutive numbers, such as 079xxx100 - 079xxx130, and all of them have free status.
Here is an example of what my table looks like:
CREATE TABLE numere
(
value int,
code varchar(10)
);
INSERT INTO numere (value,code)
Values
(123100, 'free'),
(123101, 'free'),
...
(123107, 'booked'),
(123108, 'free'),
(...
(123130, 'free'),
(123131, 'free'),
...
(123200, 'free'),
(123201, 'free'),
...
(123230, 'free'),
(123231, 'free'),
...
I need an SQL query to get the range 123200-123230 (and all of the following available ranges) in this example.
Now I have found an example doing more or less what I need:
select value, code
from numere
where value >= (select a.value
from numere a
left join numere b on a.value < b.value
and b.value < a.value + 30
and b.code = 'free'
where a.code = 'free'
group by a.value
having count(b.value) + 1 = 30)
limit 30
but this only returns the first 30 available numbers, and not within my range (0-30). (and takes 13 minutes to complete, hehe ..)
If anyone has an idea, let me know (I am using SQL Server)