Good afternoon.
I'm trying to sort a grouped result and I want to get the last move made by a client with a given ID.
The idea is to group by ID and show only the last query of each client (ID). Doing a test with 6 records of the same client (the actual query is with thousands of clients), there is no way to show me the last query of each one, it always shows me the first of each one , in this case the one dated 2016-12-31 15:27:28.
The field I order is a date type DATE_TIME.
The sentences are the following, where I get the same result in the 4 variants of GROUP BY.
Can any enlightened person give me a hand?
mysql> SELECT fecha,cliente,dni FROM base.tabla ORDER BY fecha ASC; +---------------------+---------+-----------+ | fecha | cliente | dni | +---------------------+---------+-----------+ | 2016-12-31 15:27:28 | 17553 | 77889990A | | 2017-01-01 19:09:53 | 17553 | 77889990A | | 2017-01-01 19:36:44 | 17553 | 77889990A | | 2017-01-11 01:35:35 | 17553 | 77889990A | | 2017-08-27 09:51:38 | 17553 | 77889990A | | 2017-08-31 13:07:34 | 17553 | 77889990A | +---------------------+---------+-----------+ 6 rows in set (0,00 sec) mysql> SELECT fecha,cliente,dni FROM (SELECT fecha,cliente,dni FROM base.tabla ORDER BY fecha ASC) as ultimos GROUP BY dni; +---------------------+---------+-----------+ | fecha | cliente | dni | +---------------------+---------+-----------+ | 2016-12-31 15:27:28 | 17553 | 77889990A | +---------------------+---------+-----------+ 1 row in set (0,00 sec) mysql> SELECT fecha,cliente,dni FROM (SELECT fecha,cliente,dni FROM base.tabla ORDER BY fecha ASC) as ultimos GROUP BY dni ORDER BY ultimos.fecha ASC; +---------------------+---------+-----------+ | fecha | cliente | dni | +---------------------+---------+-----------+ | 2016-12-31 15:27:28 | 17553 | 77889990A | +---------------------+---------+-----------+ 1 row in set (0,00 sec) mysql> SELECT fecha,cliente,dni FROM (SELECT fecha,cliente,dni FROM base.tabla ORDER BY fecha DESC) as ultimos GROUP BY dni ORDER BY ultimos.fecha DESC; +---------------------+---------+-----------+ | fecha | cliente | dni | +---------------------+---------+-----------+ | 2016-12-31 15:27:28 | 17553 | 77889990A | +---------------------+---------+-----------+ 1 row in set (0,00 sec) mysql> SELECT fecha,cliente,dni FROM (SELECT fecha,cliente,dni FROM base.tabla ORDER BY fecha ASC) as ultimos GROUP BY dni ORDER BY ultimos.fecha DESC; +---------------------+---------+-----------+ | fecha | cliente | dni | +---------------------+---------+-----------+ | 2016-12-31 15:27:28 | 17553 | 77889990A | +---------------------+---------+-----------+ 1 row in set (0,00 sec) mysql> SELECT fecha,cliente,dni FROM (SELECT fecha,cliente,dni FROM base.tabla ORDER BY fecha DESC) as ultimos GROUP BY dni ORDER BY ultimos.fecha ASC; +---------------------+---------+-----------+ | fecha | cliente | dni | +---------------------+---------+-----------+ | 2016-12-31 15:27:28 | 17553 | 77889990A | +---------------------+---------+-----------+ 1 row in set (0,00 sec)
Thanks in advance.
Greetings
SOLUTION: SELECT MAX (date) AS date, client AS client, dni FROM base.table GROUP BY dni ORDER BY date ASC;
It is not necessary the subquery and therefore not the ORDER BY of the same since the MAX obtains the greater result.
++ IMPORTANT ++
The problem is in the version of MYSQL 5.7.18, with the previous versions there are no problems, the subquery orders by what you request.
Tested in versions 5.1.60, 5.5.40 and 5.6.37. #