Re: Date and filling issues

From: "Osvaldo Kussama" <osvaldo(dot)kussama(at)gmail(dot)com>
To: "Christopher Crews" <isephoenix(at)yahoo(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Date and filling issues
Date: 2008-03-20 02:36:08
Message-ID: 690707f60803191936w4581b8dj75d49837be1c1892@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

2008/3/19, Christopher Crews <isephoenix(at)yahoo(dot)com>:
> 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tena Sakai 2008-03-20 02:45:02 Re: compare 2 tables in sql
Previous Message Jonah H. Harris 2008-03-19 22:39:34 Re: compare 2 tables in sql