If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Efficient best sellers list

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-20-06, 05:20
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Efficient best sellers list

I need to display a best selling product list but I'm clueless as to the best implementation. It would be either on a monthly or a weekly basis (monthly to start off with, then weekly after more sales occur). Would it be easy enough to pull the records with the most occurrences in the tables?

As an example, something like SELECT * FROM items, sales ORDER BY OCCURRENCES(item_no) DESC
Reply With Quote
  #2 (permalink)  
Old 11-20-06, 05:36
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,259
if you want a best sellers list, then the time period is failry irrelevant to the query design, althouygh it is important to the way you use the information.

I'd suggest you have a look at the SQL aggregate functions such as count

this may be of help

you can then apply your date band using a where clause

you may want tio consider a LIMIT, pulling off the top n or top n%.

So I'd expect something like
select ISBN, booktitile, author, count(ISBN) as NoSales, Sum(SalesValue) as SalesAmount from BookSales purchasedate>=xxxxxx and PurchaseDate<=yyyyyy group by ISBN order by NoSales Desc Limit zzz
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 11-20-06, 05:38
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
I've been hunting and experimenting and this seems to work perfectly for me:
$query = "SELECT *, COUNT(cat_no) AS occurences FROM sales_digital GROUP BY cat_no, trk_no, disc_no ORDER BY occurences DESC LIMIT 10";

Woo!
Reply With Quote
  #4 (permalink)  
Old 11-20-06, 07:11
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
if it seems to work, then it only seems to work

you're going to get one row in the result set for every unique combination of cat_no, trk_no, disc_no

any time you find yourself using the dreaded, evil "select star" and GROUP BY at the same time, chances are really good that you have an invalid GROUP BY statement, even though mysql will happily and merrily execute it for you anyway
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On