0

Is there an easy way of using the LIMIT clause to only pull the top N values for each different unique values? For example, if we have 30 different, numerical rows for each A, B, and C, how can we only pull up the first 5, 8, or N rows for each A, B, and C?

Thanks in advance!

Barmar
  • 741,623
  • 53
  • 500
  • 612
Greeen
  • 33
  • 4
  • Read https://stackoverflow.com/help/how-to-ask section "Help others reproduce the problem" and https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Raymond Nijland Aug 24 '18 at 18:37

1 Answers1

0

You seems want :

select t.*
from table t
where pk in (select t1.pk 
             from table t1 
             where t.value = t1.value 
             order by t1.pk -- asc/desc 
             limit 5
            );

For latest version you can use row_number() function.

Yogesh Sharma
  • 49,870
  • 5
  • 26
  • 52