Query to find contiguous ranges on a column - Mailing list pgsql-general

From Peter Hunsberger
Subject Query to find contiguous ranges on a column
Date
Msg-id cc159a4a0910131423s11d710b3n9f253236a1ee295d@mail.gmail.com
Whole thread Raw
Responses Re: Query to find contiguous ranges on a column
List pgsql-general
Given a column of data resembling the following:

col
2
3
4
5
11
12
13
14
15
16
17
18
19
23
32
33
34
35
36
37

I need a query to find the contiguous ranges within this column, in
this case returning the result set:

start, end
2, 5
11, 19
23, 23
32, 37

I have one solution that joins the table against itself and does
(among other things) a subselect looking "not exists col +1" and "not
exists col -1" on the two instances of the table to find the start and
end.  This is, as you might guess, is not very efficient (my actual
data is some 6 million+ rows) and I'm guessing there has to be
something more efficient with windowing or possibly grouping on min
and max (though I can't see how to make sure they are part of a
contiguous set).  Anyone have any ideas?

--
Peter Hunsberger

pgsql-general by date:

Previous
From: Raymond O'Donnell
Date:
Subject: Re: How to deactivate FK's to reorder fields in table?
Next
From: Tim Landscheidt
Date:
Subject: Re: Query to find contiguous ranges on a column