Search code examples
sqldatetimemariadbgreatest-n-per-group

selects the last item by date


I have 2 types of items, 1000118500 and 1000438300 and I would like to know how to bring only the last item by the newest date.

so I would have only 2 lines, the newest ones

id item       created
1  1000438300 2018-10-26 00:43:30
2  1000438300 2018-10-16 00:44:02
3  1000118500 2018-10-13 00:41:27
4  1000438300 2018-09-26 00:47:28
5  1000118500 2018-08-09 00:40:09
6  1000438300 2018-07-12 12:05:37
7  1000118500 2018-06-28 20:06:24
8  1000118500 2018-06-28 20:06:07

could anyone help? I'm using MariaDB


Solution

  • Did you try the MAX function

    SELECT item, MAX(created)
        FROM table
    GROUP BY item
    

    or see this post : How to select id with max date group by category in PostgreSQL?