Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old March 3rd, 2002, 09:49 AM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Calculating sales ranking

I was wondering if anyone has some ideas on this. Based on a typical products/orders model I need to figure out the sales rank for a given product. Now, I can't imagine a query that can achieve this (this is to be realized on Oracle), so the solution I currently imagine is storing the ranking in table, and have a trigger update the ranking (if appropriate) every time a sale is made, but am I missing something? Is it possible to derive the ranking from the data using some sort of trick? Can I model this in some other wa to achieve this?
__________________
--
Regards
André Nćss

Puritanism: The haunting fear that someone, somewhere may be having fun

Reply With Quote
  #2  
Old March 3rd, 2002, 04:53 PM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 2 m 4 sec
Reputation Power: 43
For me a typical products/orders model usually involves at least 3 tables: products,orders,and order details.

The "order_details" table of course is the line item for the main order, meaning that since an order can consist of several different products, and quantities of each, we tie that together by having a table with a row for every single item in the order, tallying up the quantities, thus there might be multiple rows, with different products, for any one order ID:
Code:
SELECT * FROM order_details;

+------+--------+----------+
| oid  | prodid | quantity |
+------+--------+----------+
|    2 |     44 |       55 |
|    2 |     23 |        8 |
|    2 |     12 |      250 |
|    3 |     44 |        2 |
|    3 |     68 |        3 |
+------+--------+----------+


You can just make a composite primary key from "oid" (Order ID), and "prodid" (Product ID), since no order should have two lines of the same item. It's up to you to determing whether you want the sales amount to appear in this table, or to be a "calculated field", thus only appearing in the view you create for invoices.

Anyway, now it becomes very simple to get rankings of products sold:
Code:
SELECT prodid,SUM(quantity) AS ranking from order_details GROUP BY prodid ORDER by ranking DESC;

This would return a list of product ids, and the total quantity sold for each product id, ordered from greatest to least. From here, it should be easy to assign a numbered ranking. If you want a ranking based on amount of sales for each product, you would obviously have to either calculate that within the ranking query, or have a column for sales amount in the order_details table. Fortunately, since the table is very compact, using only integers, your query should perform very nicely. If you just want to stop here, you can use the programming environment to take the row output and figure out a numbered rank from this query. But, if you're like me, you want to do as much inside SQL as possible: Make a view (let's call it view_rank") out of this query, and then, for any one product, it is very easy to find its current rank, by doing another query(subquery). Let's say we want the rank for product number 22:
Code:
SELECT (COUNT(*)+1) AS rank FROM view_rank WHERE ranking > (SELECT ranking FROM view_rank WHERE prodid = 22);


Now, we have just counted the number of products which have a higher ranking than product 22. If there are 5 products which have sold more than product 22, then it is sixth in rank. Yes, we add 1 to COUNT(*), because otherwise the top product would have a ranking of zero, which would be completely understandable to us computer geeks, but not to the general purchasing public .
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
  #3  
Old March 3rd, 2002, 10:31 PM
andnaess andnaess is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jul 2001
Location: Oslo
Posts: 1,516 andnaess User rank is Private First Class (20 - 50 Reputation Level)andnaess User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Ah, that's a brilliant. Thanks a bunch

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Calculating sales ranking


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway