Skip to content
Advertisement

Get the latest order_date after aggregation my rows

I have 2 tables:

  • Customers table (which has a customer_id column)
  • Orders table (which has customer_id and order_date columns)

Is there a way to fetch all orders count for each customer with the latest order date in a single query ordered by the orders count, without using correlated sub-query?

I already have this:

However, I only get the date of the first order the customer made.

Advertisement

Answer

Use the MAX() aggregation function:

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