Solved

Limit a query to 10 rows per column value

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

831 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