How To Get Last Record In Each Group In MySQL

Sometimes you may need to select most recent record or get latest record for each date,user, id or any other group. Here’s the SQL query to get last record in each group in MySQL, since there is no built-in function for it. You can also use it to select last row for each group in PostgreSQL, SQL Server & Oracle.

 

How To Get Last Record In Each Group In MySQL

Here are the steps to get last record in each group in MySQL.

Let’s say you have a tableĀ product_sales(product, order_date,sale) that contains sales data for multiple products.

mysql> create table product_sales(product varchar(255),order_date date, sale int);

mysql> insert into product_sales(product,order_date, sale)
     values('A','2020-05-01',250),
     ('B','2020-05-01',350),
     ('C','2020-05-01',1250),
     ('A','2020-05-02',450),
     ('B','2020-05-02',650),
     ('C','2020-05-02',1050),
     ('A','2020-05-03',150),
     ('B','2020-05-03',250),
     ('C','2020-05-03',1850);

mysql> select * from product_sales;
+---------+------------+------+
| product | order_date | sale |
+---------+------------+------+
| A       | 2020-05-01 |  250 |
| B       | 2020-05-01 |  350 |
| C       | 2020-05-01 | 1250 |
| A       | 2020-05-02 |  450 |
| B       | 2020-05-02 |  650 |
| C       | 2020-05-02 | 1050 |
| A       | 2020-05-03 |  150 |
| B       | 2020-05-03 |  250 |
| C       | 2020-05-03 | 1850 |
+---------+------------+------+

Bonus Read : How to Get Record with Max Value in MySQL

 

Let’s say you want to get last record in each group, that is, for each product. First we use GROUP BY to get most recent date for each group.

mysql> select product,max(order_date) from product_sales group by product;
+---------+-----------------+
| product | max(order_date) |
+---------+-----------------+
| A       | 2020-05-03      |
| B       | 2020-05-03      |
| C       | 2020-05-03      |
+---------+-----------------+

Now that we know the most recent date for each group, we join this result with our original table to get latest record by date group.

mysql> select product_sales.* from product_sales,
           (select product,max(order_date) as order_date
                from product_sales
                group by product) max_sales
             where product_sales.product=max_sales.product
             and product_sales.order_date=max_sales.order_date;
+---------+------------+------+
| product | order_date | sale |
+---------+------------+------+
| A       | 2020-05-03 |  150 |
| B       | 2020-05-03 |  250 |
| C       | 2020-05-03 | 1850 |
+---------+------------+------+

Bonus Read : How to Create View in MySQL

 

How to Select Most Recent Record for Each User

Similarly, you can select most recent record for each user or get latest record for each id. Let’s say you have the following table user_data(user_id,transaction_date,sale) with user transaction data

mysql> create table user_data(user_id int, transaction_date date, sale int);

mysql> insert into user_data(user_id,transaction_date, sale)
          values('1','2020-05-01',25),
          ('2','2020-05-01',35),
          ('3','2020-05-01',125),
          ('1','2020-05-02',40),
          ('2','2020-05-02',50),
          ('3','2020-05-02',50),
          ('1','2020-05-03',15),
          ('2','2020-05-03',25),
          ('3','2020-05-03',50);


mysql> select * from user_data;
+---------+------------------+------+
| user_id | transaction_date | sale |
+---------+------------------+------+
|       1 | 2020-05-01       |   25 |
|       2 | 2020-05-01       |   35 |
|       3 | 2020-05-01       |  125 |
|       1 | 2020-05-02       |   40 |
|       2 | 2020-05-02       |   50 |
|       3 | 2020-05-02       |   50 |
|       1 | 2020-05-03       |   15 |
|       2 | 2020-05-03       |   25 |
|       3 | 2020-05-03       |   50 |
+---------+------------------+------+

Bonus Read : How to Get New Users Per Day in MySQL

 

First, we get the latest date for each user id using GROUP BY.

mysql> select user_id,max(transaction_date) from user_data group by user_id;
+---------+-----------------------+
| user_id | max(transaction_date) |
+---------+-----------------------+
|       1 | 2020-05-03            |
|       2 | 2020-05-03            |
|       3 | 2020-05-03            |
+---------+-----------------------+

Now that we know the most recent date for each user id, we join this result with our original table to get the latest record by user group.

mysql> select user_data.* from user_data,
                (select user_id,max(transaction_date) as transaction_date
                     from user_data
                     group by user_id) max_user
                  where user_data.user_id=max_user.user_id
                  and user_data.transaction_date=max_user.transaction_date;
+---------+------------------+------+
| user_id | transaction_date | sale |
+---------+------------------+------+
|       1 | 2020-05-03       |   15 |
|       2 | 2020-05-03       |   25 |
|       3 | 2020-05-03       |   50 |
+---------+------------------+------+

Hopefully, you can get last record in each group in MySQL