From: | Peter Hunsberger <peter(dot)hunsberger(at)gmail(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Query to find contiguous ranges on a column |
Date: | 2009-10-13 21:23:55 |
Message-ID: | cc159a4a0910131423s11d710b3n9f253236a1ee295d@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
From | Date | Subject | |
---|---|---|---|
Next Message | Greg Smith | 2009-10-13 21:44:38 | Re: Re: [GENERAL] contrib/plantuner - enable PostgreSQL planner hints |
Previous Message | Raymond O'Donnell | 2009-10-13 21:10:14 | Re: How to deactivate FK's to reorder fields in table? |