Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id EEF70659F1A for ; Mon, 15 Mar 2010 19:25:54 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 40167-07 for ; Mon, 15 Mar 2010 22:25:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ew0-f227.google.com (mail-ew0-f227.google.com [209.85.219.227]) by mail.postgresql.org (Postfix) with ESMTP id 34B1F659FB6 for ; Mon, 15 Mar 2010 19:25:44 -0300 (ADT) Received: by ewy27 with SMTP id 27so2509095ewy.34 for ; Mon, 15 Mar 2010 15:25:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :from:date:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=fj5l6elTxzSCnlYKVdvze9f1q6R5O9JGgrrOEyr7wes=; b=sM7kzD/zaAp6vGS90ChWdHNUGLPx91XmjqypiU9EIsKQiIyZMD6cvkXZNg6+5wpVgS km1BWzbCjEMu9kyd+YcJ+4NRyrcFpVuyOm5ChDOK5kGwFsHOEr/KwV8vC5xYVHnnpP7s qKmbvNWSEGMzXTsbTL83nO8pGR2gDU1DPSJf8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; b=ACLgccN0vCTflIB3asuiC+Py2j9bhoBUmwjI/2qEFG74gA+j5YPMRN4/1UOyV2cQ7t 74NDnD/PVaqMN3326D4cxcEbSbXURcsbUc4NzHJx5aIKBYDDWAADk3JbrosLfylx9tzQ tDmegU1SzxwcU4aVLZO3HYLHQozMQS21B5cH4= MIME-Version: 1.0 Received: by 10.213.109.205 with SMTP id k13mr5377896ebp.44.1268691942112; Mon, 15 Mar 2010 15:25:42 -0700 (PDT) In-Reply-To: <20100308122523.50565.qmail@f5mail-237-209.rediffmail.com> References: <20100308122523.50565.qmail@f5mail-237-209.rediffmail.com> From: Dawid Kuroczko Date: Mon, 15 Mar 2010 23:25:22 +0100 Message-ID: <758d5e7f1003151525w529f7a49ha55a6e693100abc6@mail.gmail.com> Subject: Re: list of all months To: query Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 201003/94 X-Sequence-Number: 33801 On Mon, Mar 8, 2010 at 13:25, query wrote: > > Hi, > > I want to display data for all days in a month even if no data exists for= that month. Some of the days in a month might not have any data at all. Wi= th normal query, we can display days only if data exists.But I want to disp= lay rows for all days in a month with blank data for non-existing day in da= tabase. > > How can this be achieved ? Say, you have a table like: CREATE TABLE some_data ( date date NOT NULL, some_value int ); Now, You would like to print values from March 2010, even if there is no entry for some days in such a table. We need to have a list of all the days in March. We can do it with a query: SELECT date '2010-03-01' + n AS date FROM generate_series(0, date '2010-04-01' - date'2010-03-01' - 1) AS x(n)= ; Sweet, we have dates, we just need a LEFT JOIN now: SELECT date, coalesce(value, 0) AS value FROM some_data RIGHT JOIN ( SELECT date '2010-03-01' + n AS date FROM generate_series(0, date '2010-04-01' - date'2010-03-01' - 1) AS= x(n) ) AS dates USING (date); If you are running fairy recent PostgreSQL it could be written even nicer: WITH dates AS ( SELECT date '2010-03-01' + n AS date FROM generate_series(0, date '2010-04-01' - date'2010-03-01' - 1) AS= x(n) ) SELECT date, coalesce(value, 0) AS value FROM dates LEFT JOIN some_data USING (date); Two remarks: - it is fairy easy to create generate_series(date, date) function. Give it a try - its fun! :) - coalesce function will provide 0 in places where there is no data row, or value is NULL. Best regards, Dawid