pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Cosimo Streppone <cosimo@streppone.it>
To: John A Meinel <john@arbash-meinel.com>
Cc: Postgresql Performance list <pgsql-performance@postgresql.org>
Subject: Re: tricky query
Date: Tue, 28 Jun 2005 21:33:03 +0200
Message-ID: <42C1A5EF.7070505@streppone.it> (raw)
In-Reply-To: <42C16FE4.2020803@arbash-meinel.com>
References: <6EE64EF3AB31D5448D0007DD34EEB3415C2BEC@Herge.rcsinc.local>
	<42C167C6.5070108@arbash-meinel.com>
	<42C16FE4.2020803@arbash-meinel.com>

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




view thread (19+ messages)  latest in thread

Message-ID: <42C1A5EF.7070505@streppone.it>
Permalink:  ../42C1A5EF.7070505@streppone.it/
Also on:    postgresql.org/message-id/42C1A5EF.7070505@streppone.it

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: cosimo@streppone.it, john@arbash-meinel.com
  Subject: Re: tricky query
  In-Reply-To: <42C1A5EF.7070505@streppone.it>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox