troubleshooting Question

Join selecting only the the last row on the details table

Avatar of Giorgio Todeschini
Giorgio Todeschini asked on
SQLMySQL Server
9 Comments1 Solution20 ViewsLast Modified:

I have 2 MySQL tables: 

Customers (master) 

+----+------------+
| ID | Name       |
+----+------------+
|  1 | Customer 1 |
+----+------------+
|  2 | Customer 2 |
+----+------------+
| .. | ...        |
+----+------------+

Open in new window


And Offers (details). 

+----+------------+-------+-------------+--------+
| ID | CustomerID | RevId | Date        | Amount |
+----+------------+-------+-------------+--------+
|  1 |          1 | NULL  | 2021-01-01  |   1000 |
+----+------------+-------+-------------+--------+
|  2 |          1 |     1 | 2021-02-01  |   2000 |
+----+------------+-------+-------------+--------+
|  3 |          2 | NULL  | 2021-01-10  |   1200 |
+----+------------+-------+-------------+--------+
|  4 |          2 | NULL  | 2021-02-10  |   2200 |
+----+------------+-------+-------------+--------+
|  5 |          2 |     4 | 2021-03-10  |   2400 |
+----+------------+-------+-------------+--------+
| .. |            |       |             |        |
+----+------------+-------+-------------+--------+

Open in new window


The RevId field, if not NULL,  indicate that the record of the Offers table is a revision of the offer with the ID=RevId in the same table.
For example, the second record of the table here above is a revision of the first one


Now, I would like to make a query like:


SELECT * FROM Customers LEFT JOIN Offers ON Customers.ID=Offers.CustomerID

Open in new window


but, when they are a revision, I want to select only the last record of the Offers table.

The result I would like to get is the following


+----+------------+----+------------+-------+-------------+--------+
| ID | Name       | ID | CustomerID | RevId | Date        | Amount |
+----+------------+----+------------+-------+-------------+--------+
|  1 | Customer 1 |  2 |          1 |     1 | 2021-02-01  |   2000 |
+----+------------+----+------------+-------+-------------+--------+
|  2 | Customer 2 |  3 |          2 | NULL  | 2021-01-10  |   1200 |
+----+------------+----+------------+-------+-------------+--------+
|  2 | Customer 2 |  5 |          2 |     4 | 2021-03-10  |   2400 |
+----+------------+----+------------+-------+-------------+--------+

Open in new window


I'm not new in SQL language, I tried a very lot of ways but I can't able to solve this issue

Could anyone please help me?


Thanks in advance


ASKER CERTIFIED SOLUTION
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 9 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 9 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004