Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 5CDF82E0049 for ; Wed, 19 Mar 2008 23:36:13 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 61084-08 for ; Wed, 19 Mar 2008 23:36:11 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.5 Received: from mu-out-0910.google.com (mu-out-0910.google.com [209.85.134.187]) by postgresql.org (Postfix) with ESMTP id E409D2E004B for ; Wed, 19 Mar 2008 23:36:10 -0300 (ADT) Received: by mu-out-0910.google.com with SMTP id i10so1019146mue.5 for ; Wed, 19 Mar 2008 19:36:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=xU/lGEipV70CDs2wKGJBo24gZytk4yJyTg/w4B4WiSk=; b=O7j6LlT5WL3mTfyEwL+TcCJOyMppBuw1DFtNNVBJdH26QGugfvDpkfE2ZRHCIGBNUchHnCBOmFVCS6F/5A2LUhTW+kRNX951MMC3qm9UbQqBhWFESH2Hh1lrn7weDiRu4Y3DF+4uz91XJlGZeRd5cL44Xg0nebNOOUpohB1lMiA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=NGJbykr2KPSD9WT/YSZdOsVewTbqZCY0TbUN+neZCskOg/gFqHkibg1Cx5ljQl+HubRjzF5a4EXCcPIkt/vTyuMOSHmDt3CjGRqs9oct4YdwFTznIIzAJyRkhlylf2C+GhOkzYkkoboqq+aSytHPuHGPM1fCSc3uM84bTnb6pE0= Received: by 10.78.203.20 with SMTP id a20mr3398357hug.9.1205980569064; Wed, 19 Mar 2008 19:36:09 -0700 (PDT) Received: by 10.78.150.19 with HTTP; Wed, 19 Mar 2008 19:36:08 -0700 (PDT) Message-ID: <690707f60803191936w4581b8dj75d49837be1c1892@mail.gmail.com> Date: Wed, 19 Mar 2008 23:36:08 -0300 From: "Osvaldo Kussama" To: "Christopher Crews" Subject: Re: Date and filling issues Cc: pgsql-sql@postgresql.org In-Reply-To: <687096.56737.qm@web52202.mail.re2.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <687096.56737.qm@web52202.mail.re2.yahoo.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200803/128 X-Sequence-Number: 30456 2008/3/19, Christopher Crews : > Hi All, > I'm not quite sure how to phrase this, but essentially my company has me > working on some reports and I have some charts associated with the SQL > results. > > My current query is: > > select > transaction_date as date, > sum(sale_amount) as sales > from ej_transaction > where transaction_date > between current_date - Interval '1 month' and current_date > group by transaction_date > order by transaction_date asc > > The issue I'm having is that there are some dates where sales of certain > items simply don't take place. Instead of putting a date entry in the > database with a sale amount of 0, there simply is no entry for that date. I > need to make a query that will fill in the dates not found within the date > range and populate them with the sales value of 0. > > A sample of the current results data would be like > date sales > 2008-03-07 100.00 > 2007-03-10 150.00 > 2007-03-18 50.00 > > and what I'm trying to do is fill in the missing dates with sales values of > 0. > Try: SELECT s.date::date, sum(COALESCE(ej_transaction.sale_amount,0)) as sales FROM generate_series(current_date - '1 month', current_date) AS s(date) LEFT OUTER JOIN ej_transaction GROUP BY s.date ORDER BY s.date ASC; Osvaldo