I'm not sure exactly what this is called, so I'm having a hard time Googling for it.
Let's say I want to pull the details of the first order made by each customer. I know I can do something like this:
It seems a little inelegant though, and also a super common problem. Is there a built-in SQL statement or function that does this, i.e. do some grouping operation to a result set and then let you get at all the other columns in that row without grouping by those columns? So the earliest order per customer, max total spent per customer, etc?
Let's say I want to pull the details of the first order made by each customer. I know I can do something like this:
Code:
SELECT o.Customer, o.OrderDate, o.MoreStuff1, o.MoreStuff2
FROM Orders o
INNER JOIN (
SELECT Customer, MIN(OrderDate) AS OrderDate
FROM Orders
GROUP BY Customer
) a
ON o.Customer = a.Customer AND o.OrderDate = a.OrderDate
It seems a little inelegant though, and also a super common problem. Is there a built-in SQL statement or function that does this, i.e. do some grouping operation to a result set and then let you get at all the other columns in that row without grouping by those columns? So the earliest order per customer, max total spent per customer, etc?