pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Generating a range of integers in a query
4+ messages / 4 participants
[nested] [flat]

* Generating a range of integers in a query
@ 2005-07-13 09:13  Aaron Bingham <bingham@cenix-bioscience.com>
  0 siblings, 3 replies; 4+ messages in thread

From: Aaron Bingham @ 2005-07-13 09:13 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I've got an interesting problem: I need to select all possible values
of an attribute that do /not/ occur in the database.

This would be easy (in my case at least) if there were a way to
generate a table containing all integers between 1 and n, where n is
the result of a subquery.  In my case, n will be at most a few
hundred.  I would like to be able to generate this table as a
subquery.  Any ideas?

Thanks,

-- 
--------------------------------------------------------------------
Aaron Bingham
Senior Software Engineer
Cenix BioScience GmbH
--------------------------------------------------------------------




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Generating a range of integers in a query
@ 2005-07-13 10:20  Michael Glaesemann <grzm@myrealbox.com>
  parent: Aaron Bingham <bingham@cenix-bioscience.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Michael Glaesemann @ 2005-07-13 10:20 UTC (permalink / raw)
  To: Aaron Bingham <bingham@cenix-bioscience.com>; +Cc: pgsql-sql


On Jul 13, 2005, at 6:13 PM, Aaron Bingham wrote:

> This would be easy (in my case at least) if there were a way to
> generate a table containing all integers between 1 and n, where n is
> the result of a subquery.  In my case, n will be at most a few
> hundred.  I would like to be able to generate this table as a
> subquery.  Any ideas?

Take a look at generate_series(), available in v8.0 and above.

http://www.postgresql.org/docs/8.0/interactive/functions-srf.html


Michael Glaesemann
grzm myrealbox com





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Generating a range of integers in a query
@ 2005-07-13 13:22  Jim Buttafuoco <jim@contactbda.com>
  parent: Aaron Bingham <bingham@cenix-bioscience.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Jim Buttafuoco @ 2005-07-13 13:22 UTC (permalink / raw)
  To: Aaron Bingham <bingham@cenix-bioscience.com>; pgsql-sql


I use the following function which returns a date series.  You can modify it to return an int series instead

create or replace function alldates(date,date) returns setof date
as
'
declare
        s alias for $1;
        e alias for $2;
        d date;
begin
        d := s;

        while d <= e
        LOOP
                return next d;
                select d + \'1 day\'::interval into d;
        END LOOP;

        return null;
end;
'
LANGUAGE 'plpgsql'
;

select * from alldates('2004-07-01','2004-08-10');


---------- Original Message -----------
From: Aaron Bingham <bingham@cenix-bioscience.com>
To: pgsql-sql@postgresql.org
Sent: Wed, 13 Jul 2005 11:13:06 +0200
Subject: [SQL] Generating a range of integers in a query

> Hello,
> 
> I've got an interesting problem: I need to select all possible values
> of an attribute that do /not/ occur in the database.
> 
> This would be easy (in my case at least) if there were a way to
> generate a table containing all integers between 1 and n, where n is
> the result of a subquery.  In my case, n will be at most a few
> hundred.  I would like to be able to generate this table as a
> subquery.  Any ideas?
> 
> Thanks,
> 
> -- 
> --------------------------------------------------------------------
> Aaron Bingham
> Senior Software Engineer
> Cenix BioScience GmbH
> --------------------------------------------------------------------
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: Have you checked our extensive FAQ?
> 
>                http://www.postgresql.org/docs/faq
------- End of Original Message -------




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Generating a range of integers in a query
@ 2005-07-13 16:54  Scott Marlowe <smarlowe@g2switchworks.com>
  parent: Aaron Bingham <bingham@cenix-bioscience.com>
  2 siblings, 0 replies; 4+ messages in thread

From: Scott Marlowe @ 2005-07-13 16:54 UTC (permalink / raw)
  To: Aaron Bingham <bingham@cenix-bioscience.com>; +Cc: pgsql-sql

On Wed, 2005-07-13 at 04:13, Aaron Bingham wrote:
> Hello,
> 
> I've got an interesting problem: I need to select all possible values
> of an attribute that do /not/ occur in the database.
> 
> This would be easy (in my case at least) if there were a way to
> generate a table containing all integers between 1 and n, where n is
> the result of a subquery.  In my case, n will be at most a few
> hundred.  I would like to be able to generate this table as a
> subquery.  Any ideas?

Take a look here:

http://www.postgresql.org/docs/8.0/interactive/functions-srf.html

specifically, the generate_series() function.



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-07-13 16:54 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-07-13 09:13 Generating a range of integers in a query Aaron Bingham <bingham@cenix-bioscience.com>
2005-07-13 10:20 ` Michael Glaesemann <grzm@myrealbox.com>
2005-07-13 13:22 ` Jim Buttafuoco <jim@contactbda.com>
2005-07-13 16:54 ` Scott Marlowe <smarlowe@g2switchworks.com>

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