1

For Example

product_id | costhead_id | date
1020209    | 1000105     | 2020-04-01 08:58:15
1020209    | 1000182     | 2021-11-24 11:27:40
1020209    | 1000183     | 2021-12-19 16:19:55
1020210    | 1000105     | 2020-04-01 08:58:15
1020210    | 1000182     | 2021-11-24 11:27:40
1020210    | 1000183     | 2021-12-20 16:19:55

I need a unique product_id with a maximum date. Required value like below

product_id | costhead_id | date
1020209    | 1000183     | 2021-12-19 16:19:55
1020210    | 1000183     | 2021-12-20 16:19:55
Zakir Hossain
  • 388
  • 4
  • 16
  • 1
    The question seems to be a duplicate of this: https://stackoverflow.com/questions/16914098/how-to-select-id-with-max-date-group-by-category-in-postgresql – kunsterD93 Feb 23 '22 at 13:05

1 Answers1

1

You can use distinct on:

select distinct on(product_id) *
from table_name
order by product_id, date desc

Fiddle

Zakaria
  • 4,643
  • 2
  • 4
  • 29