I have a table like:

```
id  fkey  srno  remark  date
1   A001  1
2   A001  2
3   A002  1
4   A003  1 
5   A002  2

```

I want distinct latest record based on max srno like

```
2  A001  2
4  A003  1
5  A002  2

```
['https://Stackoverflow.com/questions/53405895', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4337261/']
The best way to do this in Postgres is to use `DISTINCT ON`:

```
SELECT DISTINCT ON (fkey) id, fkey, srno
FROM yourTable
ORDER BY fkey, srno DESC;

```

[![enter image description here](https://i.stack.imgur.com/LkEyJ.png)](https://i.stack.imgur.com/LkEyJ.png)

[Demo
----](https://dbfiddle.uk/?rdbms=postgres_11&fiddle=45e6379feee11ead0fbc1bf8e790a144)
You may use a subquery with an `IN` operator

```
with tab(id, fkey, srno) as 
(
 select 1,'A001',1 union all
 select 2,'A001',2 union all    
 select 3,'A002',1 union all
 select 4,'A003',1 union all    
 select 5,'A002',2   
)
select *
  from tab
 where ( fkey, srno ) in
 (
  select fkey, max(srno)
    from tab
   group by fkey
 );

id  fkey    srno
2   A001     2
4   A003     1
5   A002     2

```

[***Rextester Demo***](https://rextester.com/KPF2258)