get last date join sql 1

get last date join sql

SELECT customer.*, purchase.*
FROM customer
JOIN (
   SELECT DISTINCT ON (customer_id) *
   FROM purchase
   ORDER BY customer_id, date DESC
) purchase ON purchase.customer_id = customer.id

Here is what the above code is Doing:
1. The inner query is selecting the most recent purchase for each customer.
2. The outer query is joining the customer table to the inner query.

Similar Posts