Solved

Limit a query to 10 rows per column value

Posted on 2014-03-26
4
706 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
  • 2
4 Comments
 
LVL 76

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 76

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to take different types of Oracle backups using RMAN.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now