Re: Complicated query on very simple database - pointers sought
| From: | Benjamin Walling | Date: | Wed, 22 May 2002 13:26:59 +0000 |
| Subject: | Re: Complicated query on very simple database - pointers sought | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-19355@lists.php.net to get a copy of this message | ||
Look into Aggregate Functions. They can let you do the things that you are
looking for.
SELECT Month(OrderDate) as 'Month', Count(DISTINCT Customer) as
'NumCustomersOrdering', Count(Customer) as 'TotalOrders' FROM tbl_Orders
GROUP BY Month(OrderDate)
will give you a list of months, and the number of Customer Orders. Throw in
a distinct if you want to know how many different customers ordered. The
GROUP BY clause basically tells it when to reset the counter. You can get
sums, averages, counts, etc using aggregates.
Check this page for more information:
http://www.mysql.com/doc/G/r/Group_by_functions.html
(I mainly use MSSQL, so the syntax might be slightly different - refer to
the URL above for specifics).
"George Pitcher" <phpdevuk@yahoo.co.uk> wrote in message
news:03b701c20190$8f53fe00$630db092@HLATITUDE...
> Hi all,
>
> I want to report some figures to my colleagues.
>
> My database (2 fields: Customer, Order date) contains over 15000 records
> dating back to 1999.
>
> I want to be able to show, in a web page, the following information
>
> Month Customers placing orders Orders placed
> Average Orders/Cust Average over prev 12 months.
>
> My work with MySQL has never been this detailed.
>
> Can anyone give me any pointers?
>
> Regards
>
> George in Edinburgh
>