Skip to content
Advertisement

MySql last record from group by item_id with order by date

My database table name is ledgers and fields are id, item_id, date, …other fields

I Want the last record from (groupBy item_id order by date ASC). from each group. I tried below query

Can you guys please help.

Advertisement

Answer

You can filter with a correlated subquery:

For performance, consider an index on (item_id, date).

Another option is to use rank() (available in MySQ 8.0 only):

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement