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.
What you're trying to accomplish is known as a groupwise maximum, which can't be achieved using ORDER BY
. Instead, one must find the MAX()
and then join the result back to the table:
SELECT prd_data.* FROM prd_data NATURAL JOIN (
SELECT sub_prd_id, MAX(created_at) created_at
FROM prd_data
GROUP BY sub_prd_id
) t
See it on sqlfiddle.