Solved

Limit a query to 10 rows per column value

Posted on 2014-03-26
4
732 Views
Last Modified: 2014-03-27
Hello,

I have a query:
select id, type_id, sum(values)
from table
group by id, type_id
order by 2, 3 desc;

Open in new window


The problem is that I only want the top 20 id's ( top 20 based on the sum(values) column ) for each type_id, instead of ALL ids for each type_id.

There is an ever growing amount of type_ids (i.e. 500+), so setting up a bunch of union-ed queries isn't a desirable option.

Is there a way to do this in oracle(or postgres).  Any help if greatly appreciated.

Thank You.
0
Comment
Question by:Alex Matzinger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 39957909
In Oracle, try row_number().

something like this (untested.   just typed in):

select id, type_id, mySum
from (
select id, type_id, mySum, row_number() over(partition by type_id order by mySum desc) rn
from (
select id, type_id, sum(values) mySum
from table
group by id, type_id
)
)
where rn <= 20
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39957935
Just noticed:  Subject says top 10, question says top 20.

Whichever it is, just change "where rn <= 20" to the appropriate number.
0
 
LVL 13

Expert Comment

by:magarity
ID: 39959520
You can use a sys_refcursor to step through the list in chunks if you'd like the first 10 or 20 and then the next batch, etc. See this Ask Tom article and the first answer from Tom:
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:2829457400346889644
0
 
LVL 4

Author Closing Comment

by:Alex Matzinger
ID: 39960326
This worked perfectly for me.  Thank You for your assistance
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question