You need get the MAX()
of updated_at
, order by runs after grouping so you need to compare with the newest date
SELECT DISTINCT *
FROM members_subscriptions
WHERE status = 'active'
AND updated_at IN(
SELECT MAX(updated_at)
FROM members_subscriptions
GROUP BY subscription_id
)
ORDER BY updated_at DESC
or
SELECT DISTINCT m.*
FROM members_subscriptions m
INNER JOIN (
SELECT subscription_id, MAX(updated_at) AS updated_at
FROM members_subscriptions GROUP BY subscription_id
) AS maxdatte USING (subscription_id, updated_at);