Subject Re: [firebird-support] Re: average in group by month
Author Helen Borrie
At 05:32 AM 26/02/2004 +0000, you wrote:
>Hi
>
> > average sales for what? per month over the year? per month for
>ever? per
> > invoice?
> >
> > /hb
>
>Would kindly show me the sql statement for both options?

OK, here are a few variations (assuming Fb 1.5).

invoice table
1. invid
2. customerid
3. invdate
4. totalamount

1. For a given customer and month:

select avg(totalamount) as mthly_avg
from invoice
where customerid = :customerid
and invdate between :startdate and :enddate;

2. For all customers in a given calendar month:

select customerid, avg(totalamount) as mthy_avg
from invoice
where extract(month from invdate) = :aMonth
group by customerid;

3. For all customers, by month, for a given period:
select
extract(year from invdate) as yearno,
extract(month from invdate) as monthno,
customerid,
avg(totalamount) as mthy_avg
where invdate between :startdate and :enddate
group by 3, 1, 2
order by 3, 1, 2

/helen