I have a comment form
comment_id | when_added |
---|---|
10 | February 23, 2022 |
twenty one | February 23, 2022 |
10 | February 24, 2022 |
I need to get the count, comment_id and the latest when_added
comment_id | when_added | count |
---|---|---|
10 | February 24, 2022 | 2 |
twenty one | February 23, 2022 | 1 |
I tried this query
SELECT COUNT(*) as count, comment_id, when_added FROM comment GROUP BY comment_id, when_added ORDER BY when_added DESC;
Is there a way to group by just using comment_id?
You should only group by
comment_id
and use theMAX()
aggregate function to get the lastwhen_added
of eachcomment_id
: