Home > Enterprise >  Select 20 results per every column value
Select 20 results per every column value

Time:09-17

I prepared query that select date from table. In table I got: rank, name, citycode as columns. When I am doing something like that:

select name, citycode  
from tab20
where rank <= 20

I got resault of first 20 rows that gets rank <= 20. And Everything would be ok, but I have to show results of first 20 rows per every citystate. Is it possible to create in one query ? I was tryin union etc but it doesn't work well.

Thanks

CodePudding user response:

You would use the row_number() function. Based on the rank that would be:

select t.*
from (select t.*,
             row_number() over (partition by citycode order by rank) as seqnum
      from tab20 t
     ) t
where seqnum <= 20;
  • Related