0

my records in table LOCATION

id area
1 bangalore
2 mysore
3 bangalore
etc...,

I need to query in the table where I should get the last 15 records of each area and I'm not knowing upfront about the areas available in the table.

tried grouping but that didn't work.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
santhosh
  • 1,919
  • 3
  • 21
  • 33

1 Answers1

0

If the "last 15 records" means the largest id values, then row_number() is one method:

select t.*
from (select t.*, row_number() over (partition by area order by id desc) as seqnum
      from t
     ) t
where seqnum <= 15;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786