Skip to content
Advertisement

SQL nested query and use of MAX to extract most recent transaction and/or comment

We have a SQL database table recording customer comments (ARCMM). I want to extract the most recent comment for each customer. Some customers do not have any comments (i.e. no entries in ARCMM). The most recent comment for a customer will have the most recent date (field DATEENTR) and, for that date, the highest value of field CNTUNIQ. The query below does not work as expected. Best fix?

Query:

Sample table ARCMM data:

Results from my SQL query attempt:

Desired results:

Advertisement

Answer

You could use row_number() within the left join, if your database supports window functions:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement