I have two tables. One is basic customer information and one is ordering information.
I'm trying to find the max(order date) with the order status of 'placed'or'cancelled'. I don't care if the status if placed or cancelled. I just want the most recent order.
The first table (Info)
CustomerID LAST NAME FIRST NAME
1 AB BOB
2 BC ROBERT
3 AA JOHN
The second table(order)
CustomerID Order Date Order Status
1 12/16/2016 placed
2 8/5/2016 cancelled
1 5/8/2015 cancelled
2 8/9/2016 placed
3 7/15/2016 cancelled
3 8/20/2015 placed
I want the result to be:
CustomerID FirstName LastName OrderDate OrderStatus
1 AB BOB 12/16/2016 placed
2 BA ROBERT 8/9/2016 placed
3 AA JOHN 7/15/2016 cancelled
Here are my SQL syntax
SELECT distinct Info.CustomerID, Info.Lastname,Info.Firstname
FROM INFO
INNER JOIN
(SELECT
order.CustomerID, LastOrderDate=max(OrderDate),order.OrderStatus
FROM Order
GROUP BY order.CustomerID, order.orderstatus)a
ON a.CustomerID=Info.CustomerID
This didn't work because it's grouping by order status which give me the max date of each all orderstatus. Then I tried
SELECT distinct Info.CustomerID, Info.Lastname,Info.Firstname, Order.OrderStatus
FROM INFO, Order
INNER JOIN
(SELECT
order.CustomerID, LastOrderDate=max(OrderDate)
FROM Order
GROUP BY order.CustomerID)a
ON a.CustomerID=Info.CustomerID
This didn't work either because it says the Info.CustomerID could not be bound.
Any helps? Thanks!