Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > MySQL
|
MySQL General discussion about the MySQL database.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the MySQL section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 25th, 2003, 10:27 PM
Authorized User
 
Join Date: Oct 2003
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to select Max

i successfully list out the customer that buy my product every month using mysql. But i can't list out the customer that have the highest sales for every month. please help. Below are my sql command.

select month(s.sales_date), s.cust_id, sum((p.prod_price * sd.quantity)) as Total
from product p, sales_detail sd, sales s, category c
where sd.sales_id=s.sales_id and p.prod_id=sd.prod_id and c.category_id=p.prod_cat and year(s.sales_date) = '1999'
group by month(s.sales_date), s.cust_id;

+---------------------+---------+-------+
| month(s.sales_date) | cust_id | Total |
+---------------------+---------+-------+
| 1 | C001 | 134 |
| 1 | C004 | 162 |
| 2 | C001 | 390 |
| 2 | C020 | 354 |
| 3 | C001 | 522 |
| 3 | C002 | 153 |
| 3 | C013 | 93 |
| 3 | C014 | 50 |
| 3 | C020 | 321 |
| 4 | C001 | 493 |
| 4 | C012 | 33 |
| 4 | C013 | 36 |
| 4 | C014 | 197 |
| 5 | C010 | 487 |
| 5 | C011 | 461 |
| 5 | C020 | 430 |
| 6 | C001 | 667 |
| 6 | C020 | 289 |
| 7 | C001 | 44 |
| 7 | C019 | 178 |
| 7 | C020 | 78 |
| 8 | C001 | 125 |
| 9 | C001 | 144 |
| 9 | C002 | 78 |
| 9 | C003 | 36 |
| 10 | C001 | 250 |
| 10 | C018 | 304 |
| 10 | C019 | 210 |
| 10 | C020 | 308 |
| 11 | C001 | 346 |
| 11 | C020 | 68 |
| 12 | C001 | 203 |
| 12 | C002 | 220 |
|+---------------------+---------+----

i try to code it like this but to no avail. please help
select month(s.sales_date), s.cust_id, Max(sum((p.prod_price * sd.quantity)) )as Total
from product p, sales_detail sd, sales s, category c
where sd.sales_id=s.sales_id and p.prod_id=sd.prod_id and c.category_id=p.prod_cat and year(s.sales_date) = '1999'
group by month(s.sales_date), s.cust_id;






Similar Threads
Thread Thread Starter Forum Replies Last Post
select Max 2 rows for each ID veeruu SQL Language 1 January 31st, 2006 05:08 AM
Select two MAX record per each veeruu SQL Language 2 January 23rd, 2006 12:57 AM
How to select 5th Max in a Table ramk_1978 SQL Language 2 December 30th, 2004 01:41 AM
Ani 1 noe how to retrieve record from select max() babymeizi Pro JSP 2 October 14th, 2004 05:11 PM
how to select Max hosefo81 PHP Databases 0 November 21st, 2003 12:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.