I want to group below data from sub_prd_id
. but before that i need order data from max created_at
.
I wrote query as below.
Your hunch was correct. This will so it:
select * from
(select * from prd_data order by created_at desc) x
group by sub_prd_id
Note that this is a mysql only solution, but since the question was tagged mysql that should be OK. Mysql has special functionality regarding group by when only some of the non-aggregated columns are grouped-by: whereas all other databases disallow this, mysql returns the first row encountered for each unique group by combination. By sorting before grouping, you get the rows with the latest created_at value for each sub_prd_id.