Re: Generating a range of integers in a query

From: "Jim Buttafuoco" <jim(at)contactbda(dot)com>
To: Aaron Bingham <bingham(at)cenix-bioscience(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Generating a range of integers in a query
Date: 2005-07-13 13:22:32
Message-ID: 20050713132129.M45696@contactbda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)cenix-bioscience(dot)com>
To: pgsql-sql(at)postgresql(dot)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 -------

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message David Blankley 2005-07-13 15:28:29 Dynamic Offset Determination
Previous Message Graham Vickrage 2005-07-13 11:31:29 Re: Copy user privileges