Date and filling issues

Поиск
Список
Период
Сортировка
Искать

Date and filling issues

От:
Christopher Crews <isephoenix@yahoo.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.

Thanks,
-CC


Never miss a thing. Make Yahoo your homepage.

Re: Date and filling issues

От:
"Osvaldo Kussama" <osvaldo.kussama@gmail.com>
Дата:
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

Re: Date and filling issues

От:
"Rodrigo E. De León Plicet" <rdeleonp@gmail.com>
Дата:
On 3/19/08, Christopher Crews  wrote:
> and what I'm trying to do is fill in the missing dates with sales values of 0.

create or replace function gen_dates(sd date, ed date)
returns setof date as $$
select $1 + i
from generate_series(0, $2 - $1) i;
$$ language sql immutable;

select d.date, sum(coalesce(sale_amount,0)) as sales
from gen_dates((current_date - interval '1 month')::date, current_date) d(date)
left join ej_transaction
on transaction_date=d.date
group by d.date
order by d.date asc;

Re: Date and filling issues

От:
chester c young <chestercyoung@yahoo.com>
Дата:

> 
> 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.

what I do is have a table called days that is the left side of a left
join.

I'm not proud of this, but it works well.


     ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping

FAQ