DBAsupport.com Forums - Powered by vBulletin
Results 1 to 3 of 3

Thread: limiting a group by to the first 10 groups

  1. #1
    if I do,
    select colA, sum(colB)
    from tableA
    group by colA
    order by sum(colB) desc

    how do I get ONLY the first 10 groups (those with the largest sums)...

    and DON'T tell me to do this:

    select colA, sum(colB)
    from tableA
    where rownum < 11
    group by colA
    order by sum(colB) desc

    because this will reduce the rows used in the grouping, not the groups itself.

  2. #2
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    Try this:
    select colA, sum(colB)
    from tableA
    where rownum < 11
    group by colA
    order by sum(colB) desc

    ;)

    No, really, try this:
    select rownum, cola, scb
    from (select colA, sum(colb) scb from table a group by cola order by scb desc)
    where rownum <= 10
    Jeff Hunter

  3. #3
    Uh, doesn't the inner select statement still bring back all of the groups...

    ok, maybe this will work, but as for efficiency, it may not be all that great... perhaps if I use a FIRST_ROWS hint... maybe then...

    i'll try it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width