Solved

Limit a query to 10 rows per column value

Posted on 2014-03-26
4
737 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

728 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