From: | Cosimo Streppone <cosimo(at)streppone(dot)it> |
---|---|
To: | John A Meinel <john(at)arbash-meinel(dot)com> |
Cc: | Postgresql Performance list <pgsql-performance(at)postgresql(dot)org> |
Subject: | Re: tricky query |
Date: | 2005-06-28 19:33:03 |
Message-ID: | 42C1A5EF.7070505@streppone.it |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-performance |
John A Meinel wrote:
> John A Meinel wrote:
>> Merlin Moncure wrote:
>>
>>> I need the smallest integer that is greater than zero that is not in the
>>> column of a table. In other words, if an 'id' column has values
>>> 1,2,3,4,6 and 7, I need a query that returns the value of 5.
>>
>> [...]
>
> Well, I was able to improve it to using appropriate index scans.
> Here is the query:
>
> SELECT t1.id+1 as id_new FROM id_test t1
> WHERE NOT EXISTS
> (SELECT t2.id FROM id_test t2 WHERE t2.id = t1.id+1)
> ORDER BY t1.id LIMIT 1;
I'm very interested in this "tricky query".
Sorry John, but if I populate the `id_test' relation
with only 4 tuples with id values (10, 11, 12, 13),
the result of this query is:
cosimo=> create table id_test (id integer primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'id_test_pkey'
for table 'id_test'
CREATE TABLE
cosimo=> insert into id_test values (10); -- and 11, 12, 13, 14
INSERT 7457570 1
INSERT 7457571 1
INSERT 7457572 1
INSERT 7457573 1
INSERT 7457574 1
cosimo=> SELECT t1.id+1 as id_new FROM id_test t1 WHERE NOT EXISTS (SELECT
t2.id FROM id_test t2 WHERE t2.id = t1.id+1) ORDER BY t1.id LIMIT 1;
id_new
--------
15
(1 row)
which if I understand correctly, is the wrong answer to the problem.
At this point, I'm starting to think I need some sleep... :-)
--
Cosimo
From | Date | Subject | |
---|---|---|---|
Next Message | Merlin Moncure | 2005-06-28 19:36:29 | Re: tricky query |
Previous Message | Bruno Wolff III | 2005-06-28 19:31:23 | Re: tricky query |