I’m trying to build an sql query with no success. As I’m a newbie i hope to find your help.
Scenery: a store is organizing an award among its customers. Here is the database they have:
__________________________________________________________________
id | name | date | time | item | section |
__________________________________________________________________
01 | J. Smith | 2011-10-30 | 10:12:56 | shoes | sport |
02 | J. Smith | 2011-10-27 | 11:14:19 | T-shirt | sport |
03 | M. Douglas | 2011-11-01 | 04:00:42 | keyboard | computer |
04 | R. Obrian | 2010-12-31 | 11:33:57 | webcam | computer |
05 | M. Douglas | 2011-01-07 | 12:42:00 | webcam | computer |
06 | J. Smith | 2011-02-05 | 02:30:51 | T-shirt | sport |
07 | J. Smith | 2011-05-12 | 06:55:14 | bike | sport |
08 | J. Smith | 2011-03-15 | 09:59:22 | shoes | sport |
09 | J. Smith | 2011-09-09 | 08:10:36 | webcam | computer |
10 | J. Smith | 2011-06-09 | 10:00:38 |microphone| computer |
I want to make a top 10 list of customers for each section, with the following criteria: the best buyer is who bought the major number of DIFFERENT items. In case of same results (same number of different items bought) among customers, the best buyer is who 1st (date & time) bought all his items.
I’d like to have, for example, the following top 10 results table:
SPORT
____________________________________________________________
| name | items bought (different) | Date & Time |
____________________________________________________________
| XXXYYY | 16 | 2011-10-05 @07:15|
| ZZZZTT | 16 | 2011-10-05 @08:10|
| QQHAIJ | 16 | 2011-10-06 @10:35|
| TTTSFT | 15 | 2011-11-03 @12:22|
| LIUREN | 14 | 2011-11-14 @16:10|
| ODIRNU | 9 | 2011-07-03 @14:18|
| OIXUED | 8 | 2011-11-20 @03:03|
| SUHDKO | 7 | 2011-02-02 @22:51|
| TNODJH | 7 | 2011-06-10 @12:55|
| ENOSJR | 6 | 2011-04-04 @11:39|
_____________________________________________________________
The queries must be different for each section, id est i must have a different string (and table of results) for SPORT, COMPUTER…et cetera.
Who can help me? Thanks in advance!
Bob.
Early first item purchaser wins
Example table result:
Early last item purchaser wins
Example table result: