Suppose i have a mysql table name table with fields
- rank
- date
- id
The values are like:
10, 2012-01-01, 3
9, 2012-01-04, 3
5, 2012-01-07, 3
3, 2012-01-10, 3
10, 2012-01-01, 4
6, 2012-01-04, 4
7, 2012-01-07, 4
In a single sql, how can i get both last and first values sorted by date grouped by id?
I know how to get first one or last one
SELECT rank, id FROM
(SELECT rank, id FROM table ORDER BY date DESC) s GROUP BY id;
I would like that the fields returned to be somethink like: lastrank, firstrank and id.
Thank you
Try this:
The result of this query taking your sampla data as input is: