I have two tables, a customers
and orders
table.
The customers
table contains a unique ID
for each customer. It contains 1141 entries.
The orders
table contains many entries with a customerID
and a date
.
I am trying to query my database and return a list of customers and the max(date)
from the orders list.
SELECT *
FROM customers
INNER JOIN
(
SELECT CustomerID, max(date) as date
FROM orders
GROUP BY CustomerID
) Sub1
ON customers.id = Sub1.CustomerID
INNER JOIN orders
ON orders.CustomerID = Sub1.CustomerID
AND orders.date = Sub1.Date
However this query is returning 1726 rows instead of 1141 rows. Where is this getting extra from?