We help IT Professionals succeed at work.
Get Started

PL/SQL Limit number of rows returned by query

Last Modified: 2019-12-19
Hello expert,

Need to discover Proctocol_ID records in the History table that have a lot of records and look at deleting them.
Wrote query copied herewith below. Does the job but it returns 950k rows. Examining the data it seen that if the the records that have count  > 20 or RowNum < 4000 a more manageable number, say 4k
would be returned. Tried limiting it  by count and Rownum, as shown in the commented lines in query
but no joy, probably because the group by clause does not allow a where limiter.

How can I get  the group by to work but limit the number of rows to thousands instead of hundreds of thousands?


Allen in Dallas

    with CTE as (
 select hpr.protocol_id from CCM.H_Protocol hpr
 join CCM_protocol pro on pro.protocol_id =  hpr.protocol_id
 where pro.is_disabled = 0)
 select protocol_id, count(*) as Num
 from CTE
--where rownum <4000
--where Num > 20
  group by protocol_id
--where rownum <4000  
--where Num > 20
  order by Num desc
Watch Question
Database Administrator / Software Engineer
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE