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 Nth Rows of a Group By

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-03, 09:36
BrunoLopes BrunoLopes is offline
Registered User
 
Join Date: Jul 2003
Location: Lisbon, Portugal
Posts: 1
Top Nth Rows of a Group By

Hi ...

I have a table called tblProducts (idProduct, idCategory, Price)

I want to build a single query wich returns the Nth products with higher price for each category.

It's probably quite simple, but I can't manage to get it ...
Maybe it's because I'm tired tired tired ...

Thanks a lot!!

Bruno Lopes
Reply With Quote
  #2 (permalink)  
Old 10-02-03, 07:03
itsmanyam itsmanyam is offline
Registered User
 
Join Date: May 2003
Posts: 45
Re: Top Nth Rows of a Group By

Try this

select idproduct,max(price) from tblproducts
group by idproduct



Quote:
Originally posted by BrunoLopes
Hi ...

I have a table called tblProducts (idProduct, idCategory, Price)

I want to build a single query wich returns the Nth products with higher price for each category.

It's probably quite simple, but I can't manage to get it ...
Maybe it's because I'm tired tired tired ...

Thanks a lot!!

Bruno Lopes
Reply With Quote
  #3 (permalink)  
Old 10-02-03, 10:05
fhunth fhunth is offline
Registered User
 
Join Date: Oct 2002
Location: Argentina
Posts: 72
Try this

select * from (
select idproduct,max(price),rownum from tblproducts
group by idproduct)

where rownum < 11

will return then top 10 records
Reply With Quote
  #4 (permalink)  
Old 10-04-03, 18:00
alberto.dellera alberto.dellera is offline
Registered User
 
Join Date: Sep 2003
Location: Milan, Italy
Posts: 130
Re: Top Nth Rows of a Group By

Quote:
Originally posted by BrunoLopes
Hi ...

I have a table called tblProducts (idProduct, idCategory, Price)

I want to build a single query wich returns the Nth products with higher price for each category.

It's probably quite simple, but I can't manage to get it ...
Maybe it's because I'm tired tired tired ...

Thanks a lot!!

Bruno Lopes
Do you use Oracle 8i + ? For N = 2

SQL> create table tblProducts (idProduct int, idCategory int, price int);

Table created.

SQL> insert into tblProducts select rownum, mod(rownum,2), 100*rownum from all_objects where rownum <= 8;

8 rows created.

SQL> select * from tblProducts order by IDCATEGORY,PRICE desc;

IDPRODUCT IDCATEGORY PRICE
---------- ---------- ----------
8 0 800
6 0 600
4 0 400
2 0 200
7 1 700
5 1 500
3 1 300
1 1 100

8 rows selected.

SQL> select IDCATEGORY, IDPRODUCT, price from (
2 select IDCATEGORY, IDPRODUCT, price, rank() over (partition by IDCATEGORY order by price desc) r
3 from tblproducts
4 ) where r <= 2;

IDCATEGORY IDPRODUCT PRICE
---------- ---------- ----------
0 8 800
0 6 600
1 7 700
1 5 500

You can use dense_rank() instead of rank(), depending on what you would do if you have e.g. 2 products with price = 800, and 1 with price=700 in category 0 - do you return 2 or 3 records ?
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