Query return only last record (transaction) of each customer and product?

Options

I have the following scenario:

DATABASE TRANSACTION:
1- CLIENT A - PRODUCT A
2- CLIENT A - PRODUCT A
3- CLIENT B - PRODUCT A
4 - CLIENT C - PRODUCT B
5- CLIENT D - PRODUCT A
6- CLIENT D - PRODUCT A

Every time the customer tries to buy and the purchase is refused, it generates a transaction, but it is for the same product.

How do I return only the last transaction of the same CLIENT and the same PRODUCT. In the case of the above records it should only return:

2- CLIENT A - PRODUCT A
3- CLIENT B - PRODUCT A
4 - CLIENT C - PRODUCT B
6- CLIENT D - PRODUCT A

Can I do this filter in the database?

Comments

  • Sean Montgomery
    Sean Montgomery Administrator

    ADMIN

    Options
     If I understand you correctly, you can do this with an aggregate and then use addons on top of that.

    1. query all records in aggregate mode on the transaction table with a group by client and product.
    2. use an aggregator of MAX on the transaction id so you now get client_id, product_id, and max_transaction_id
    3. use addons to turn client_id and product_id into their objects

    Does that make sense?