X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 882DAD728E for ; Tue, 11 Oct 2005 15:35:29 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 25887-08 for ; Tue, 11 Oct 2005 18:35:24 +0000 (GMT) Received: from floppy.pyrenet.fr (floppy.pyrenet.fr [194.116.145.2]) by svr1.postgresql.org (Postfix) with ESMTP id 6968CD722C for ; Tue, 11 Oct 2005 15:35:24 -0300 (ADT) Received: by floppy.pyrenet.fr (Postfix, from userid 106) id F168931059; Tue, 11 Oct 2005 20:35:24 +0200 (MET DST) From: david@fetter.org (David Fetter) Subject: Re: generating a sequence table against which to do a LEFT OUTER JOIN X-Newsgroups: pgsql.sql References: <1128722342.139760.195020@g47g2000cwa.googlegroups.com> User-Agent: tin/1.7.8-20050321 ("Hellisay") (UNIX) (Linux/2.6.13-1.1526_FC4 (i686)) Message-ID: Date: Tue, 11 Oct 2005 13:35:21 -0500 Lines: 41 X-Complaints-To: abuse@speakeasy.net X-DMCA-Complaints-To: abuse@speakeasy.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 required=5 tests=[none] X-Spam-Level: X-Archive-Number: 200510/194 X-Sequence-Number: 23047 Andrew Hammond wrote: > So I need an end result that has entries for all days, even when > there's nothing happening on those days, generate from a timestamped > event table. I've already got the interesting entries. Now I need to > fill the holes. > > To do this, I'm thinking a LEFT OUTER JOIN against a date sequence > table. So, how do I generate a table with every day from A to B? > > Or am I going about this the wrong way? What you have is fine, but you're doing extra work. There's this neat function in PostgreSQL 8.0 or better (you can write one for earlier versions) called generate_series(). > SELECT pop_days('2005-01-01'::date, '2005-02-01'::date); -- barfs. SELECT '2005-01-01'::date + s.i * '1 day'::interval AS "Date", t.your_date_col FROM generate_series(0,'2005-02-01'::date - '2005-01-01'::date - 1) AS s(i); LEFT JOIN your_table t ON ('2005-01-01'::date + s.i = t.your_date_col); You can also use generate_series() with a correllated subquery so as not to have to hard-code dates. HTH :) Cheers, D -- David Fetter david@fetter.org http://fetter.org/ phone: +1 510 893 6100 mobile: +1 415 235 3778 When a man tells you that he got rich through hard work, ask him: 'Whose?' Don Marquis, quoted in Edward Anthony, O Rare Don Marquis