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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Top Five Of Each Group

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-27-04, 20:35
EMEN24 EMEN24 is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
Talking Top Five Of Each Group

I want to figure how I can get the top 3 products sold for each product class. I have table as the following. (This an example, I have alot more products and a lot product classes)

Product Class Product Sold
Glass A 13
Glass B 10
Glass C 15
Glass D 7
Glass E 12
Wood F 15
Wood G 17
Wood H 5
Wood I 0
Wood J 10

I want the result to come out like this..


Product Class Product Sold
Glass C 15
Glass A 13
Glass E 12
Wood G 17
Wood F 15
Wood J 10

Can anyone please help me?
Reply With Quote
  #2 (permalink)  
Old 09-28-04, 01:55
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
How do you want to deal with "ties", for example:
Code:
Product Sold	Product Class
12		Thingie
15		Thingie
7		Thingie
12		Thingie
12		Thingie
12		Thingie
7		Thingie
15		Thingie
7		Thingie
Would it produce:
Code:
Product Sold	Product Class
15		Thingie
12		Thingie
7		Thingie
or maybe
Code:
Product Sold	Product Class
15		Thingie
15		Thingie
12		Thingie
12		Thingie
12		Thingie
12		Thingie
7		Thingie
7		Thingie
7		Thingie
Each of these are valid answers, using different rules for how to count the "three" part!

-PatP
Reply With Quote
  #3 (permalink)  
Old 09-28-04, 13:01
EMEN24 EMEN24 is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
I would like it to show

Product Sold Product Class
15 Thingie
15 Thingie
12 Thingie
12 Thingie
12 Thingie
12 Thingie
7 Thingie
7 Thingie
7 Thingie

of course it being the top 3, it would look like this:

Product Sold Product Class
15 Thingie
15 Thingie
12 Thingie
Reply With Quote
  #4 (permalink)  
Old 09-28-04, 14:20
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Based on the data that you've presented, I don't think there is a way to do that without using iteration of some kind. If you could add a unique column to the table, then it would be possible.

-PatP
Reply With Quote
  #5 (permalink)  
Old 09-28-04, 15:14
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
SELECT Product_Class, Product_Sold
FROM table t1
WHERE 3 > (SELECT count(*) FROM table t2 WHERE t1.Product_Sold < t2.Product_Sold and t2.Product_Class = t1.Product_Class)
Reply With Quote
  #6 (permalink)  
Old 09-28-04, 16:55
EMEN24 EMEN24 is offline
Registered User
 
Join Date: Sep 2004
Posts: 3
urquel,
That work beautifuly. The part that is pissing me off is I am trying to figure out the logic behind it. I think I am getting a brain cramp. Well, it doesn't matter, as long as it worked. Thanks!
Reply With Quote
  #7 (permalink)  
Old 09-29-04, 10:03
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Just think of it like this. For each row in the query (t1), are there fewer than 3 rows in the correlated subquery (t2) that have a greater value. If there are, you qualify for the top 3 list.
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