I'm trying to get rows that have max value in each group.
I have this tables
+-------+-----------+--------------+-----------+
| nid | name | crated | grp_id |
+-------+-----------+--------------+-----------+
| 1 | RAND_NAME | 123 | 11 |
| 2 | EHllo | 111 | 11 |
| 3 | Stop by | 444 | 11 |
| 4 | Radr c | 555 | 11 |
| 5 | NAE | 666 | 22 |
| 6 | ABC | 1234 | 22 |
| 7 | RAND | 123 | 22 |
| 8 | YELLO | 444 | 22 |
| 9 | AAA | 555 | 33 |
| 10 | WWW | 1235 | 33 |
| 11 | ADF | 553 | 33 |
+-------+-----------+--------------+-----------+
So, I want this table
+-------+-------------+------------+-----------+
| nid | name | created | grp_id |
+-------+-------------+------------+-----------+
| 3 | Radr c | 555 | 11 |
| 6 | ABC | 1234 | 22 |
| 10 | WWW | 1235 | 33 |
+-------+-------------+------------+-----------+
which means I want to grab rows that have the highest created value in each group. The table will be grouped by grp_id.
I was thinking this way:
SELECT nid, name, created, grp_id
FROM table t
WHERE t.created = (SELECT MAX(t1.created) FROM table t1)
GROUP BY grp_id
ORDER BY grp_id
But, it didn't work out. What should I do to get three different rows that have the highest created value in each group?
Thank you for understanding about my poor explanation.